[英]Dynamically Allocated Memory in Structure Array (in C)
我在弄清楚如何动态地将内存分配给数组结构时遇到麻烦。 我需要使用已定义结构的数组,以便可以对其进行迭代以打印信息。 但是,还要求我动态地将内存分配给数组。
问题是,当我在代码中使用malloc()时(您会看到它已被注释掉),它将指针从数组索引中移开。 有什么方法可以在指向数组索引存储数据时完成动态分配内存的工作?
免责声明:我对C还是陌生的,如果我的编码伤害了您的眼睛,我谨此致歉。 只是想了解程序而不是快速完成它。
程序说明:
编写一个使用“结构”来定义包含以下学生的结构的C程序:
信息:
您的程序应为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;
}
试试这个。...在这里,我声明一个结构指针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;
}
使用数组数组...(代替使用单独的分配,您可以使用循环为每个学生分配值)
#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;
}
如果您使用指针读取和打印结构值,则不需要数组;如果您尝试使用数组,则可以使用此方法。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.