【发布时间】:2018-03-13 03:55:26
【问题描述】:
我有一个程序用于获取输入并将其存储到结构中。这些结构用于联系详细信息、姓名、地址和电话号码。我的程序运行良好,我可以将所有信息输入到程序中,但是当我尝试printf 时,结果程序在中途崩溃。我认为它们可能是内存或损坏或其他问题。我认为它可能与损坏有关的原因是,如果我切断我的一些程序并编译它,而不是程序崩溃,我会得到一个“运行时检查失败#2 变量 '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 错误窗口,而不是编译器窗口。
【问题讨论】:
-
StreetNumber 是 char,但您输入为字符串:
scanf("%s", &addresses.streetNumber); -
编译时,始终启用警告,然后修复这些警告。 (对于
gcc, at a minimum use:-Wall -Wextra -Wconversion -pedantic -std=gnu11`)这会导致编译器输出10个警告,主要是关于调用scanf()的格式字符串,其中输入/格式说明符与数据不匹配设置。 -
在调用任何
scanf()系列函数时 1) 始终检查返回值以确保操作成功。 2) 使用%s和/或%[...]说明符时,始终包含一个最大字符修饰符,该修饰符比输入缓冲区的长度小1,因为这些说明符始终将NUL 字节附加到输入。这也避免了缓冲区溢出及其导致的未定义行为和可能的段错误事件的任何可能性。 -
关于:
scanf(" %c", &addresses.apartmentNumber);我的公寓号码是764,它不适合单个字符。事实上,几乎所有公寓号码的长度都是多个字符,可以是:22A,所以不是严格的数字。 -
关于:
char postalCode[8];对于许多邮政编码来说,这还不够长。例如在美国,邮政编码由一个 5 位数字、一个空格或破折号、一个 4 位数字组成。一个 8 字符的字段只够容纳 5 位数字、NUL 终止符和另外 2 个字符。