C中的分段错误

发布于 2024-10-06 07:53:16 字数 552 浏览 6 评论 0原文

我正在尝试编写这段代码,但运行程序后出现分段错误,您能帮忙解决一下吗?

#include <stdio.h>
#include <string.h>

typedef struct{
    int salary;
    char* name;
} employee ;

int main(){
    employee p[2];
        int i;
        for(i=0;i<2; i++){
            printf("enter sal ");
            scanf("%d", &p[i].salary);

           printf("enter name ");
           scanf("%s", &p[i].name);
        }
        for(i=0;i<2; i++){
           printf("p %d",p[i].salary);
           printf("p %s",p[i].name);
        }
    return 0;
}

I am trying to write this code, but it gives me segmentation fault after running the program, could you please help to sort it out?

#include <stdio.h>
#include <string.h>

typedef struct{
    int salary;
    char* name;
} employee ;

int main(){
    employee p[2];
        int i;
        for(i=0;i<2; i++){
            printf("enter sal ");
            scanf("%d", &p[i].salary);

           printf("enter name ");
           scanf("%s", &p[i].name);
        }
        for(i=0;i<2; i++){
           printf("p %d",p[i].salary);
           printf("p %s",p[i].name);
        }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沐歌 2024-10-13 07:53:17

您必须为 employee 每个实例的 name 成员保留内存:

p[i].name = (char*)malloc(expected_max_size);

就在该变量的 scanf 之前。声明指向 char char* 的指针不会为指向的实际字符串分配内存,而只是为指针本身分配内存,并且在您的示例中它未初始化。通过使用malloc,您可以保留一块内存并使指针指向它。您必须小心边界检查,因为您必须事先保留内存,足以容纳用户正在写入的内容。

You have to reserve memory for the name member of each instance of employee:

p[i].name = (char*)malloc(expected_max_size);

just before the scanf for that variable. Declaring a pointer to char char* does not assign memory for the actual string pointed to, but just for the pointer itself, and in your example it is not initialized. By using malloc you reserve a piece of memory and makes the pointer point to it. You have to be careful with bounds checking, because you have to reserve the memory beforehand, enough to hold what the user is writing.

悲凉≈ 2024-10-13 07:53:17

您需要为结构中的“name”字符串分配内存。使用 malloc() 或通过将 name 声明为给定大小 (char name[SIZE]) 的数组来执行此操作。

You need to allocate memory for the "name" string in your structure. Do this using malloc() or by declaring name as an array of a given size (char name[SIZE]).

々眼睛长脚气 2024-10-13 07:53:16
  • 您需要为名称字段分配内存:p[i].name = (char*)malloc(MAX_NAME_LEN)
  • 另外,scanf("%s", &p[i ].name) 应为 scanf("%s", p[i].name)
  • You need to allocate memory for the name field: p[i].name = (char*)malloc(MAX_NAME_LEN)
  • Also, the scanf("%s", &p[i].name) should read scanf("%s", p[i].name).
怀念你的温柔 2024-10-13 07:53:16

结构体字段name只是一个通配符指针。

char* name;

您正在将用户输入读取为:

scanf("%s", &p[i].name);

name 指向的内存中,该内存可以位于任何位置。

要解决此问题,您需要动态分配 name 指向的内存,或者可以将 name 更改为大小比最大值大一的 char 数组名称的长度可能。

char name[MAX];

The structure field name is just a wild character pointer.

char* name;

you are reading the user input as:

scanf("%s", &p[i].name);

into the memory pointed by name which could be anywhere.

To fix this you need to dynamically allocate memory pointed to by name or you can change name to a char array of size one greater than the max length of the name possible.

char name[MAX];
黎夕旧梦 2024-10-13 07:53:16

你不需要 &扫描到指针时的运算符。并且您需要 malloc p[i].name

       scanf("%s", p[i].name);

You don't need the & operator when scanf'ing to pointer. And you need to malloc p[i].name

       scanf("%s", p[i].name);
滴情不沾 2024-10-13 07:53:16

您没有为 char* name 分配内存。更改数据结构

typedef struct
{ 
    int salary; 
     char name[50]; 
} 

或使用 malloc 分配内存

You are not allocating memory for char* name. change your data structure to

typedef struct
{ 
    int salary; 
     char name[50]; 
} 

or allocate memory using malloc

浊酒尽余欢 2024-10-13 07:53:16

您忘记为 p[i].name 分配内存。

You forgot to allocate memory for p[i].name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文