c - 程序在编译快要结束时崩溃

标签 c struct scanf

我有一个程序可以接收输入并将其存储到结构中。这些结构用于联系方式、姓名、地址和电话号码。我的程序工作正常,我可以将所有信息输入到程序中,但是当我尝试 printf 结果时,程序在中途崩溃了。我认为它们可能是内存或损坏或其他问题。我认为这可能与腐败有关的原因是,如果我切断我的一些程序并编译它,而不是程序崩溃,我会得到一个“运行时检查失败#2 Stack around the variable 'optionAddress' 已损坏错误。这是我的程序

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "contacts.h"
int main(void)
{
    // Declare variables here:

    struct Name names;
    char optionName;
    struct Address addresses;
    char optionAddress;
    struct Numbers number;
    char optionCell;
    char optionHome;
    char optionBusiness;

    // Display the title

    printf("Contact Management System\n");
    printf("-------------------------\n");

    // Contact Name Input:

    printf("Please enter the contact's first name: ");
    scanf("%s", &names.firstName);
    printf("Do you want to enter a middle initial(s)? (y or n): ");
    scanf("%s", &optionName);
    while (optionName == 'y' || optionName == 'Y') {
        printf("Please enter the contact's middle initial(s): ");
        scanf("%s", &names.middleInitial);
        break;
    }
    printf("Please enter the contact's last name: ");
    scanf("%s", &names.lastName);

    // Contact Address Input:

    printf("Please enter the contact's street number: ");
    scanf("%s", &addresses.streetNumber);
    printf("Please enter the contact's street name: ");
    scanf("%s", &addresses.street);
    printf("Do you want to enter an apartment number? (y or n): ");
    scanf("%s", &optionAddress);
    while (optionAddress == 'y' || optionAddress == 'Y') {
        printf("Please enter the contact's apartment number: ");
        scanf(" %c", &addresses.apartmentNumber);
        break;
    }
    printf("Please enter the contact's postal code: ");
    scanf("%s", &addresses.postalCode);
    printf("Please enter the contact's city: ");
    scanf("%s", &addresses.city);

    // Contact Numbers Input:

    printf("Do you want to enter a cell phone number? (y or no): ");
    scanf("%s", &optionCell);
    while (optionCell == 'y' || optionCell == 'Y') {
        printf("Please enter the contact's cell phone number: ");
        scanf(" %c", number.cell);
        break;
    }
    printf("Do you want to enter a home phone number? (y or n): ");
    scanf("%s", &optionHome);
    while (optionHome == 'y' || optionHome == 'Y') {
        printf("Please enter the contact's home phone number: ");
        scanf(" %c", &number.home);
        break;
    }
    printf("Do you want to enter a business phone number? (y or n): ");
    scanf("%s", &optionBusiness);
    while (optionBusiness == 'y' || optionBusiness == 'Y') {
        printf("Please enter the contact's business phone number: ");
        scanf(" %c", number.business);
        break;
    }

    // Display Contact Summary Details

    printf("Contact Details\n");
    printf("---------------\n");
    printf("Name Details\n");
    printf("First name: ");
    printf("%s", names.firstName);
    printf("\nMiddle initials(s): ");
    printf("%s", names.middleInitial);
    printf("\nLast name: ");
    printf("%s", names.lastName);
    printf("\n\nAddress Details\n");
    printf("Street number: ");
    printf("%s", addresses.streetNumber);
    printf("\nStreet name: ");
    printf("%s", addresses.street);
    printf("\nApartment: ");
    printf("%s", addresses.apartmentNumber);
    printf("\nPostal code: ");
    printf("%s", addresses.postalCode);
    printf("\nCity: ");
    printf("%s", addresses.city);
    printf("\n\nPhone Numbers: ");
    printf("\nCell phone number: ");
    printf("%s", number.cell);
    printf("\nHome phone number: ");
    printf("%s", number.home);
    printf("\nBusiness phone number: ");
    printf("%s", number.business);

    // Display Completion Message

    printf("\n\nStructure test for Name, Address, and Numbers Done!");

    return 0;
}

以及头文件中的结构体:

// Structure type Name declaration
struct Name {
    char firstName[31];
    char middleInitial[7];
    char lastName[36];
};

// Structure type Address declaration
// Place your code here...

 struct Address {
    char streetNumber;
    char street[41];
    char apartmentNumber;
    char postalCode[8];
    char city[41];
 };

 // Structure type Numbers declaration
 // Place your code here...

 struct Numbers {
    char cell[21];
    char home[21];
    char business[21];   
 };

我的程序打印出 "Street number: " 然后停止工作。显示的是 Windows 错误窗口,而不是编译器窗口。

最佳答案

My program reaches the point of printing out "Street number: " then stops working.

看看:

 struct Address {
    char streetNumber;    // declared as character!
    char street[41];
    char apartmentNumber; // character
    char postalCode[8];
    char city[41];
 };

char optionName;
char optionAddress;
char optionCell;
char optionHome;
char optionBusiness;

稍后您尝试读取字符的值:

scanf("%s", &optionName);
scanf("%s", &optionAddress);
scanf("%s", &optionCell);
scanf("%s", &optionHome);
scanf("%s", &optionBusiness);

scanf("%s", &addresses.streetNumber); // <-------------- string read

使用字符串格式 %s。这会调用 UB,因为您正在读取至少 2 字节。 "y" 是一个带有字符串空终止符 '\0' 的字符串。那个额外的字节会覆盖内存位置。 将变量声明更改为字符串或读取格式更改为字符读取,就像您在此处所做的那样:

  scanf(" %c", &addresses.apartmentNumber);

关于c - 程序在编译快要结束时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49248093/

相关文章:

c - 如何查找 char** 数组中的元素个数?

c - 如何使用线程更改函数中的结构变量?

c - sscanf 是否被认为可以安全使用?

转换函数以从字符串而不是 C 中的文件读取

c - 输出负整数到 %u 格式说明符

c - 这个错误是什么意思: `somefile.c:200: error: the frame size of 1032 bytes is larger than 1024 bytes` ?

c - EVP_CIPHER_CTX_set_key_length 接受河豚的错误大小

c++ - 如果我们只有一个成员,结构和 union 之间有什么区别吗?

c - 对指针没有影响的语句

c - 当某些字段为空时,如何使用 sscanf 和 scanset 以逗号分隔的字符串提取字段