如何在C中实现动态数组?

如何在C中实现动态数组?,c,dynamic-arrays,dynamic-allocation,C,Dynamic Arrays,Dynamic Allocation,我正在使用动态内存分配编写此代码,对于学生记录,如图所示,此代码假定很简单,我显然是以正确的方式将元素分配到正确的位置,但当涉及到打印它们时,它会给我一个“核心转储”错误!怎么了 #include <stdio.h> #include <stdlib.h> #include <string.h> void main() { char **firstname; char **lastname; float *score; int

我正在使用动态内存分配编写此代码,对于学生记录,如图所示,此代码假定很简单,我显然是以正确的方式将元素分配到正确的位置,但当涉及到打印它们时,它会给我一个“
核心转储
”错误!怎么了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    char **firstname;
    char **lastname;
    float *score;
    int number_of_records,i,j,ctr=1,row=15,col=20;
    /*ctr is to keep track of the student's number (makes it easier to
      the user), it starts with (1)*/

    firstname=malloc(row*sizeof(char*));
    for(i=0;i<row;i++)
    {
        firstname[i]=malloc((col+1)*sizeof(char));
    }
     lastname=malloc(row*sizeof(char*));
    for(i=0;i<row;i++)
    {
        lastname[i]=malloc((col+1)*sizeof(char));
    }
    printf("\nPlease indicate number of records you want to enter (min 2, max 15): ");
    scanf("%d",&number_of_records);
    score=malloc(row*sizeof(float));

    printf("\nPlease input records of students\n(enter a new line after"
           "each record), with following format:\nfirst name last name score ");
    for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",firstname[i],lastname[i],score[i]);

        ctr++; /*ctr is to keep track of student number
                 (makes it easy to the user) */

    }
     for (i=0;i<number_of_records;i++)
    {

        printf("%s %s %f\n",firstname[i],lastname[i],score[i]);
    }
}
#包括
#包括
#包括
void main()
{
字符**名字;
字符**姓氏;
浮动*分数;
记录的整数,i,j,ctr=1,行=15,列=20;
/*ctr是跟踪学生的电话号码(使其更容易
用户),它以(1)开头*/
firstname=malloc(row*sizeof(char*));
对于(i=0;i请更改此项:

for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",&firstname[i],&lastname[i],&score[i]);

        ctr++; /*ctr is to keep track of student number (makes it easy to the       user) */
    }

for(i=0;i删除&在输入信息时给我一个“core dumped”错误。原因是
malloc()的返回值
不应被铸造,请参见@aero您能给我们您尝试过的触发错误的确切输入吗?到目前为止,我没有复制它。我没有收到错误。请编辑您的问题并添加您当前的代码,以防您做了任何意外更改。@aero仔细阅读答案。删除
firstname[I]的
lastname[i]
但不是
score[i]
for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",firstname[i],lastname[i],&score[i]);

        ctr++; /*ctr is to keep track of student number (makes it easy to the       user) */
    }