问题: Struct在编辑后会将数据保留在其中。
我有一个程序,它管理结构中的联系人,出于某种原因,当我更新联系人时,如果我选择不编辑它,它不会重置或将该字段留空。
更新功能:
void updateContact(struct Contact contact[], int update) {
char contactInput[11] = { '\0' };
int ans = 0;
int contactIndex;
int contactx;
printf("Enter the cell number for the contact: ");
getTenDigitPhone(contactInput);
contactIndex = findContactIndex(contact, update, contactInput);
contactx = findContactIndex(contact, update, contactInput);
if (contactx == -1) {
printf("*** Contact NOT FOUND ***\n");
}
else {
printf("\nContact found:\n");
displayContact(&contact[contactIndex]);
printf("\nDo you want to update the name? (y or n): ");
ans = yes();
if (ans == 1) {
getName(&contact[contactIndex].name);
}
printf("Do you want to update the address? (y or n): ");
ans = yes();
if (ans == 1) {
getAddress(&contact[contactIndex].address);
}
printf("Do you want to update the numbers? (y or n): ");
ans = yes();
if (ans == 1) {
getNumber(&contact[contactIndex].numbers);
}
}
}
STRUCT:
struct Contact contact[MAXCONTACTS] =
{ { { "Rick", { '\0' }, "Grimes" },
{ 11, "Trailer Park", 0, "AJA 2J2", "King City" },
{ "4161112222", "2162223333", "4163334444" } },
{ { "Maggie", "R.", "Greene" },
{ 55, "Hightop House", 0, "A9A 3K3", "Bolton" },
{ "9051112222", "9052223333", "9053334444" } },
getNumber功能:
void getNumber(struct Number *num) {
int response;
printf("Please enter the contact's cell phone number: ");
getTenDigitPhone(num->cell);
// scanf("%10s", num->cell);
// *num->cell = getInt();
printf("Do you want to enter a home phone number? (y or n): ");
response = yes();
if (response == 1) {
printf("Please enter the contact's home phone number: ");
getTenDigitPhone(num->home);
// scanf("%10s", num->home);
// *num->home = getInt();
}
printf("Do you want to enter a business phone number? (y or n): ");
response = yes();
if (response == 1) {
printf("Please enter the contact's business phone number: ");
getTenDigitPhone(num->business);
// scanf("%10s", num->business);
// *num->business = getInt();
} }
我想在更新联系人后将该字段留空(如果我没有更新)。我不知道如何删除成员结构,所以请帮助我,我将非常感激。
答案 0 :(得分:0)
将struct字段设置为空指针,或指向“my blank string”的指针
*num->cell = 0;
如果你这样做,你将不得不在以后检查null。
或者您可以执行*num->cell = myBlankString;
,其中空白字符串定义为
char myBlankString[1] = "";
这是一个可打印的空白字符串。