结构 c 的分段错误

问题描述 投票:0回答:2
#include "stdio.h"

typedef struct
{
    char name[50];
    int money;
    char IBAN[50];
    char passoword[20];
    char email[30];
}account;

void create_account(account *ptr)
{
    printf("****Welcome to account resgister****\n");
    printf("Whats your name?: \n");
    scanf(" %49s",ptr->name);
    printf("Whats your email? : ");
    scanf(" %29s",ptr->email);
    printf("Type you password (from 4 to 20): ");
    scanf(" %19s",ptr->passoword);
}

int menu()
{
    char option = 0;
    while(option != '0' && option != '1' && option != '2')
    {
        printf("****MENU****\n");
        printf("0 - Exit\n");
        printf("1 - Create Account\n");
        printf("2 - Log in\n");
        printf("What you want to do: \n");
        scanf(" %c", &option);
    }
    return option;
}

int main (void)
{
    char option;
    account *ptr;

    while(option != '0')
    {
        option = menu();

        switch (option)
        {
        case '0':
            break;
        case '1':
            create_account(ptr);
            break;
        }
    }
}

有人可以告诉我,当我在菜单中选择选项 1 时,然后当我要求写我的名字时,我写下它,他们在行中给了我一个分段错误:scanf(" %49", ptr->name)。我不知道为什么会发生这种情况,就像我有 %50s 之前一样,我查找它并更改为 %49s 以匹配我的数组大小,但错误仍然存在。谢谢

c struct segmentation-fault
2个回答
0
投票
#include "stdio.h"

typedef struct
{
    char name[50];
    int money;
    char IBAN[50];  
    char password[20];
    char email[30];
} account;

void create_account(account *ptr)
{
    printf("****Welcome to account register****\n");
    printf("What's your name?: \n");
    scanf(" %49s", ptr->name);
    printf("What's your email?: "); 
    scanf(" %29s", ptr->email);
    printf("Type your password (4 to 20 chars): ");
    scanf(" %19s", ptr->password); 
}

int menu()
{
    char option = 0;
    while(option != '0' && option != '1' && option != '2') {
        printf("****MENU****\n");
        printf("0 - Exit\n");
        printf("1 - Create Account\n");
        printf("2 - Log in\n");
        printf("What you want to do: \n");
        scanf(" %c", &option);
    }
    return option;
}

int main(void)
{
    char option;
    account *ptr = malloc(sizeof(account));
    
    while(option != '0') {
        option = menu();

        switch(option) {
            case '0': 
                break;
            case '1':
                create_account(ptr);
                break;
        }
    }
    
    free(ptr);
    return 0;
}

试试这个


0
投票

测试代码时,第一个线索是编译器警告,指出您的结构指针可能未初始化。

/home/craig/C_Programs/Console/Struct/main.c|53|warning: ‘ptr’ may be used uninitialized [-Wmaybe-uninitialized]|

确实,这是代码的基本问题,因为当我运行代码时,我也遇到了分段错误。检查您的代码,在帐户创建函数中,没有为您的结构分配内存。考虑到这一点,以下是添加内存分配的最简单的重构。

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

typedef struct
{
    char name[50];
    int money;
    char IBAN[50];
    char passoword[20];
    char email[30];
} account;

void create_account(account *ptr)
{
    ptr = malloc(sizeof(account));  /* Allocates a block of memory for the structure */
    printf("****Welcome to account resgister****\n");
    printf("Whats your name?: \n");
    scanf(" %49s",ptr->name);
    printf("Whats your email? : ");
    scanf(" %29s",ptr->email);
    printf("Type you password (from 4 to 20): ");
    scanf(" %19s",ptr->passoword);
}

这样,在终端运行测试确实允许创建帐户。

craig@Vera:~/C_Programs/Console/Struct/bin/Release$ ./Struct 
****MENU****
0 - Exit
1 - Create Account
2 - Log in
What you want to do: 
1
****Welcome to account resgister****
Whats your name?: 
Craig
Whats your email? : [email protected]
Type you password (from 4 to 20): Hello
****MENU****
0 - Exit
1 - Create Account
2 - Log in
What you want to do: 
0

要注意的主要事情是要认识到您可能收到的任何编译器警告,因为它们经常指出可能的未定义行为,而且您可能想深入研究一些“C”教程文献,因为它与内存分配有关诸如结构之类的物体。

© www.soinside.com 2019 - 2024. All rights reserved.