![]() |
#2
rjsp2022-10-12 09:26
把题目贴出来,因为你使用了很多内存分配,我不知道这是题目的要求,还是你从Java等语言带来的恶习
![]() #include <stdio.h> #include <string.h> #include <stdlib.h> struct teacher { char* name; char** students; }; void teachers_alloc_3x4( struct teacher** pTeachers ) { *pTeachers = malloc( 3 * sizeof(struct teacher) ); for( size_t i=0; i!=3; ++i ) { (*pTeachers)[i].name = malloc( 64 ); sprintf( (*pTeachers)[i].name, "teacher_%zu", 1+i ); (*pTeachers)[i].students = malloc( 4 * sizeof(char*) ); for( size_t j=0; j!=4; ++j ) { (*pTeachers)[i].students[j] = malloc( 64 ); sprintf( (*pTeachers)[i].students[j], "%s_student_%zu", (*pTeachers)[i].name, 1+j ); } } } void teachers_free_3x4( struct teacher* pTeachers ) { for( size_t i=0; i!=3; ++i ) { for( size_t j=0; j!=4; ++j ) free( pTeachers[i].students[j] ); free( pTeachers[i].students ); free( pTeachers[i].name ); } free( pTeachers ); } void teachers_output_3x4( const struct teacher* teachers ) { for( size_t i=0; i!=3; ++i ) { puts( teachers[i].name ); for( size_t j=0; j!=4; ++j ) printf( " %s\n", teachers[i].students[j] ); } } void test01( void ) { struct teacher* teachers; teachers_alloc_3x4( &teachers ); teachers_output_3x4( teachers ); teachers_free_3x4( teachers ); } int main( void ) { test01(); } 假如不是题目的要求,那 ![]() #include <stdio.h> #include <string.h> struct teacher { char name[64]; char students[4][64]; }; void teachers_init( struct teacher teachers[], size_t n ) { for( size_t i=0; i!=n; ++i ) { sprintf( teachers[i].name, "teacher_%zu", 1+i ); for( size_t j=0; j!=4; ++j ) sprintf( teachers[i].students[j], "%s_student_%zu", teachers[i].name, 1+j ); } } void teachers_output( const struct teacher teachers[], size_t n ) { for( size_t i=0; i!=n; ++i ) { puts( teachers[i].name ); for( size_t j=0; j!=4; ++j ) printf( " %s\n", teachers[i].students[j] ); } } void test01( void ) { struct teacher teachers[3]; teachers_init( teachers, 3 ); teachers_output( teachers, 3 ); } int main( void ) { test01(); } |

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//结构体嵌套二级指针练习
//结构体设计
struct TeaCher {
//老师姓名
char *name;
//老师带的学生姓名数组
char **Studets;
};
void allocateSpace(struct TeaCher ***teacherArray) {
if (teacherArray == NULL)
{
return;
}
//堆区分配内存
struct TeaCher ** ts = malloc(sizeof(struct TeaCher *) * 3);
//数据赋值
for (int i = 0; i < 3; i++)
{
ts[i] = malloc(sizeof(struct TeaCher));//给老师分配内存
ts[i]->name = malloc(sizeof(char) * 64);//给老师姓名属性分配内存
sprintf(ts[i]->name, "TeaCher_%d", i + 1);//给老师姓名赋值
//给老师带领学生数组分配内存
ts[i]->name = malloc(sizeof(char *)* 4);
//给学生姓名赋值
for (int j = 0; j < 4; j++)
{
ts[i]->Studets[j] = (char)malloc(sizeof(char)* 64);
sprintf(ts[i]->Studets[j], "%s_studet_%d", ts[i]->name, j + 1);
}
}
//建立关系
*teacherArray = ts;
}
//打印操作
void printTeacharArray(struct TeaCher ** teacherArray) {
for (int i = 0; i < 3; i++)
{
printf("%s\n", teacherArray[i]->name);//老师姓名
for (int j = 0; j < 4; j++)
{
printf(" %s\n",teacherArray[i]->Studets[j]);
}
}
}
void test01() {
//老师数组创建
struct TeaCher ** teacherArray = NULL;
//分配内存
allocateSpace(&teacherArray);
//打印所有老师和学生的信息
printTeacharArray(teacherArray);
}
int main() {
test01();
system("pause");
return EXIT_SUCCESS;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//结构体嵌套二级指针练习
//结构体设计
struct TeaCher {
//老师姓名
char *name;
//老师带的学生姓名数组
char **Studets;
};
void allocateSpace(struct TeaCher ***teacherArray) {
if (teacherArray == NULL)
{
return;
}
//堆区分配内存
struct TeaCher ** ts = malloc(sizeof(struct TeaCher *) * 3);
//数据赋值
for (int i = 0; i < 3; i++)
{
ts[i] = malloc(sizeof(struct TeaCher));//给老师分配内存
ts[i]->name = malloc(sizeof(char) * 64);//给老师姓名属性分配内存
sprintf(ts[i]->name, "TeaCher_%d", i + 1);//给老师姓名赋值
//给老师带领学生数组分配内存
ts[i]->name = malloc(sizeof(char *)* 4);
//给学生姓名赋值
for (int j = 0; j < 4; j++)
{
ts[i]->Studets[j] = (char)malloc(sizeof(char)* 64);
sprintf(ts[i]->Studets[j], "%s_studet_%d", ts[i]->name, j + 1);
}
}
//建立关系
*teacherArray = ts;
}
//打印操作
void printTeacharArray(struct TeaCher ** teacherArray) {
for (int i = 0; i < 3; i++)
{
printf("%s\n", teacherArray[i]->name);//老师姓名
for (int j = 0; j < 4; j++)
{
printf(" %s\n",teacherArray[i]->Studets[j]);
}
}
}
void test01() {
//老师数组创建
struct TeaCher ** teacherArray = NULL;
//分配内存
allocateSpace(&teacherArray);
//打印所有老师和学生的信息
printTeacharArray(teacherArray);
}
int main() {
test01();
system("pause");
return EXIT_SUCCESS;
}