c用户输入中的结构数组

c用户输入中的结构数组,c,C,一般来说,我不熟悉编程,尤其是C。 我试图编写一个使用结构数组的程序,但如果该结构包含字符串,我会遇到问题。 在用户给出最后一次输入后,编译器不知何故崩溃了 下面的结构只是一个只包含一项的简化版本,因为问题似乎是将字符串读入数组。 非常感谢您的帮助,提前谢谢 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char* name; }student;

一般来说,我不熟悉编程,尤其是
C
。 我试图编写一个使用结构数组的程序,但如果该结构包含字符串,我会遇到问题。 在用户给出最后一次输入后,编译器不知何故崩溃了

下面的结构只是一个只包含一项的简化版本,因为问题似乎是将字符串读入数组。 非常感谢您的帮助,提前谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
    char* name;
}student;

int main()
{
    int size;
    printf("enter number of entries\n");
    scanf("%d" , &size);
    student* all=malloc(size*sizeof(student));

    int i;
    for(i=0;i<size;i++)
    {
        printf("enter name\n");
        scanf("%s" , all[i].name);
    }

    return 0;
}
#包括
#包括
#包括
类型定义结构
{
字符*名称;
}学生;
int main()
{
整数大小;
printf(“输入条目数\n”);
scanf(“%d”,大小(&S);
学生*all=malloc(大小*sizeof(学生));
int i;

对于(i=0;i在输入
scanf(“%s”,all[i].name);
,您需要将内存分配给
all[i].name

一个例子-

for(i=0;i<size;i++)
{
    all[i].name=malloc(20*sizeof(*(all[i].name)));
    if(all[i].name!=NULL){
       printf("enter name\n");
       scanf("%19s" , all[i].name);
    }
}
//use these strings
for(i=0;i<size;i++){
       free(all[i].name);                  //free the allocated memory 
}
free(all);

没有为学生姓名分配内存(
char*name
),因此当试图
scanf
指向该指针时,访问无效内存,程序崩溃

最简单的方法是将
name
声明为数组:
charname[28];

malloc()
的返回值也需要检查,以防为
学生分配内存时出现问题,这将返回一个
空指针
。最后,需要使用
free()
释放分配的内存

例如:

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

typedef struct {
    char name[28];
    unsigned int age;
} student;

int main()
{
    size_t size = 0;
    printf("\nEnter number of entries: ");
    scanf("%zu", &size);
    // add some check for size

    student* students = (student*)malloc(size * sizeof(student));

    if (students == NULL) {
        printf("\nProblem with allocating memory:\n"
               " - size: %zu\n"
               " - total size needed: %zu\n",
               size, size * sizeof(student));
        return 0;
    }

    for (size_t i = 0; i < size; ++i) {
        printf("Enter name: ");
        scanf("%27s", students[i].name);
        printf(" Enter age: ");
        scanf("%u", &students[i].age);
    }

    printf("\nList of students:\n");

    for (size_t i = 0; i < size; ++i) {
        printf("%s (%u)\n", students[i].name, students[i].age);
    }

    free(students); // free the allocated memory

    return 0;
}
#包括
#包括
类型定义结构{
字符名[28];
无符号整数;
}学生;
int main()
{
大小\u t大小=0;
printf(“\n输入条目数:”);
scanf(“%zu”、&size);
//添加一些尺寸检查
学生*学生=(学生*)malloc(大小*大小(学生));
如果(学生==NULL){
printf(“\n内存分配问题:\n”
“-大小:%n\n”
“-需要的总大小:%n\n”,
尺码,尺码*尺码(学生));
返回0;
}
对于(大小i=0;i
不要忘记
释放
内存,并且在将此更改添加到
长无符号整数
大小
时不会产生任何其他错误。非常好,您包含了问题的最小版本,而不是原始代码!!!这使得这是一个比仅仅转储更好的问题g不管你有什么资料。记住在以后的问题上也要这样做。我们仍然迫切需要一个“当我试图将内容转储到未初始化指针的地址时程序崩溃”的规范副本:(@iharob提到了这一点。在使用
scanf
while(getchar()!='\n')后,不要忘记清除输入缓冲区);
我编辑了你的答案:
scanf
的格式说明符也错了。@d3l-谢谢,我更新了格式说明符(虽然并非所有(旧的)编译器都支持
%zu”
)。我认为这里不需要清除
\n
,因为我们没有扫描字符。
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char name[28];
    unsigned int age;
} student;

int main()
{
    size_t size = 0;
    printf("\nEnter number of entries: ");
    scanf("%zu", &size);
    // add some check for size

    student* students = (student*)malloc(size * sizeof(student));

    if (students == NULL) {
        printf("\nProblem with allocating memory:\n"
               " - size: %zu\n"
               " - total size needed: %zu\n",
               size, size * sizeof(student));
        return 0;
    }

    for (size_t i = 0; i < size; ++i) {
        printf("Enter name: ");
        scanf("%27s", students[i].name);
        printf(" Enter age: ");
        scanf("%u", &students[i].age);
    }

    printf("\nList of students:\n");

    for (size_t i = 0; i < size; ++i) {
        printf("%s (%u)\n", students[i].name, students[i].age);
    }

    free(students); // free the allocated memory

    return 0;
}