二维数组

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

typedef struct _Ateacher {
	int id;
	char *name;
	char **stu;
}Teacher_t;

Teacher_t *createTeacher(int n)
{
	int i = 0, j = 0;
	Teacher_t *st = NULL;
	
	st = (Teacher_t *)malloc(n*sizeof(Teacher_t));
	for (i = 0; i < n; i++)
	{
		st[i].name = (char *)malloc(100);
		
		st[i].stu = (char **) malloc(sizeof(char *));
		for (j = 0; j < n; j++)
		{
			st[i].stu[j] = (char *)malloc(100);
		}
	}
	
	return st;
}

void printst(Teacher_t *st)
{
	int i = 0, j = 0;
	
	for( i = 0; i < 3; i++)
	{
		printf("Tname: %s, Tid: %d 	 ->>> stu info", st[i].name, st[i].id);
	
		for ( j = 0; j < 3; j ++)
		{
			printf("	Sname%d: %s", j, st[i].stu[j]);
		}
		printf("
");
	}
}

int main()
{
	Teacher_t *st = NULL;
	int i = 0, j = 0;

	st = createTeacher(3);
	for( i = 0; i < 3; i++)
	{
		st[i].id = i;
		printf("input name [%d]: ", sizeof(st[i].name));
		scanf("%s", st[i].name);
		
		printf("input stu info
");
		for ( j = 0; j < 3; j ++)
		{
			printf("	[%d]input name%d: ",  sizeof(st[i].stu[j]), j);
			scanf("%s", st[i].stu[j]);
		}
	}
	printst(st);
	
	return 0;
}

  

二维指针及结构体使用

原文地址:https://www.cnblogs.com/porkerface/p/12031584.html