我无法弄清楚如何动态地将内存分配给数组结构。我需要使用我定义的结构数组,以便我可以迭代它来打印信息。但是,我也被要求动态地为数组分配内存。
问题是当我在代码中使用malloc()时(你会看到它被注释掉)它会从数组索引中抛出指针。有没有办法在指向数组指示存储数据的同时动态分配内存?
免责声明:我是C的新手,如果我的编码伤到你的眼睛,我会提前道歉。只是想了解程序而不是快速完成它。计划说明:
编写一个使用' struct'的程序。定义包含以下学生的结构:
信息:
您的课程应为5名学生打印上述信息。
提示:使用结构数组并遍历数组以打印信息。
使用以下内容创建结构
检查内存是否可用,然后为您的结构动态分配所需的内存。
为结构变量赋值并将其打印到控制台。
到目前为止代码:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main (void)
{
struct student
{
char initial [21];
int age;
float id;
char grade [3];
} list[5];
struct student * tag = list;
//LINE BELOW IS WHAT I WAS TRYING TO DO; BUT THROWS POINTER OFF OF STRUCT ARRAY
//tag = ( struct student * ) malloc ( sizeof ( struct student ));
strcpy(tag->initial, "KJ");
tag->age = 21;
tag->id = 1.0;
strcpy (tag->grade, "A");
tag++;
strcpy(tag->initial, "MJ");
tag->age = 55;
tag->id = 1.1;
strcpy (tag->grade, "B");
tag++;
strcpy(tag->initial, "CJ");
tag->age = 67;
tag->id = 1.2;
strcpy (tag->grade, "C");
tag++;
strcpy(tag->initial, "SJ");
tag->age = 24;
tag->id = 1.3;
strcpy (tag->grade, "D");
tag++;
strcpy(tag->initial, "DJ");
tag->age = 27;
tag->id = 1.4;
strcpy (tag->grade, "F");
tag++;
int n;
for ( n = 0; n < 5; n++ ) {
printf ( "%s is %d, id is %f, grade is %s\n",
list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade);
}
return 0;
}
答案 0 :(得分:0)
试试这个....这里我宣布一个结构指针tag
。我为5个学生结构分配内存。分配值后,现在tag
指向最后一个值。所以我将它递减5次。这个程序只使用一个指针。如果你想,你可以尝试使用指针数组。
我在程序中提到了//changes
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main (void)
{
struct student
{
char initial [21];
int age;
float id;
char grade [3];
} list[5];
struct student * tag;
tag = ( struct student * ) malloc (5* sizeof (struct student));//changes
strcpy(tag->initial, "KJ");
tag->age = 21;
tag->id = 1.0;
strcpy (tag->grade, "A");
tag++;
strcpy(tag->initial, "MJ");
tag->age = 55;
tag->id = 1.1;
strcpy (tag->grade, "B");
tag++;
strcpy(tag->initial, "CJ");
tag->age = 67;
tag->id = 1.2;
strcpy (tag->grade, "C");
tag++;
strcpy(tag->initial, "SJ");
tag->age = 24;
tag->id = 1.3;
strcpy (tag->grade, "D");
tag++;
strcpy(tag->initial, "DJ");
tag->age = 27;
tag->id = 1.4;
strcpy (tag->grade, "F");
tag++;
int n;
tag=tag-5;//changes
for ( n = 0; n < 5; n++ ) {
printf ( "%s is %d, id is %f, grade is %s\n",
tag->initial, tag->age, tag->id, tag->grade);
tag++;//changes
}
return 0;
}
使用pinter数组...(而不是使用单独的赋值,你可以使用循环为每个学生分配值)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
struct student
{
char initial [21];
int age;
float id;
char grade [3];
} list[5];
struct student *tag[5];
int i;
for(i=0;i<5;i++)
tag[i]= ( struct student * ) malloc (sizeof (struct student));
strcpy(tag[0]->initial, "KJ");
tag[0]->age = 21;
tag[0]->id = 1.0;
strcpy (tag[0]->grade, "A");
strcpy(tag[1]->initial, "MJ");
tag[1]->age = 55;
tag[1]->id = 1.1;
strcpy (tag[1]->grade, "B");
strcpy(tag[2]->initial, "CJ");
tag[2]->age = 67;
tag[2]->id = 1.2;
strcpy (tag[2]->grade, "C");
strcpy(tag[3]->initial, "SJ");
tag[3]->age = 24;
tag[3]->id = 1.3;
strcpy (tag[3]->grade, "D");
strcpy(tag[4]->initial, "DJ");
tag[4]->age = 27;
tag[4]->id = 1.4;
strcpy (tag[4]->grade, "F");
for ( i = 0; i < 5; i++ )
{
printf ( "%s is %d, id is %f, grade is %s\n",
tag[i]->initial, tag[i]->age, tag[i]->id, tag[i]->grade);
}
return 0;
}
答案 1 :(得分:0)
如果您使用指针来读取和打印结构值,则不需要数组。如果您尝试使用数组,则可以使用此方法。
#include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main (void)
{
struct student
{
char initial [21];
int age;
float id;
char grade [3];
} list[5];
struct student *tag = ( struct student * ) malloc ( sizeof ( struct student ) * 5);
int i;
for(i=0;i<5;i++)
{
printf("Enter the student initial\n");
scanf("%s",list[i].initial);
printf("Enter the student age\n");
scanf("%d",&list[i].age);
printf("Enter the student id\n");
scanf("%f",&list[i].id);
printf("Enter the grade\n");
scanf("%s",list[i].grade);
tag++;
}
int n;
for ( n = 0; n < 5; n++ ) {
printf ( "%s is %d, id is %f, grade is %s \n",
list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade);
}
return 0;
}