[英]Deleting a member of a struct in C
Problem: Struct keeps the data in it after being edited. 问题:结构在编辑后将数据保留在其中。
I have a program where it manages Contacts in a struct and for some reason when I update a contact, it doesn't reset or leave the field blank if I choose not to edit it. 我有一个程序可以在结构中管理“联系人”,由于某种原因,当我更新联系人时,如果我选择不对其进行编辑,它不会重置或将该字段保留为空白。
Update function: 更新功能:
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 Function: 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();
} }
I want to leave the field blank (if I didn't update it) after I update the contact. 更新联系人后,我想将该字段保留为空白(如果未更新)。 I don't know how to delete a member struct so please help me, I will really appreciate it.
我不知道如何删除成员结构,所以请帮助我,我将非常感谢。
Set the struct field to a null pointer, or a pointer to "my blank string" 将结构字段设置为空指针或指向“我的空白字符串”的指针
*num->cell = 0;
If you do this, you'll have to check for null later. 如果这样做,则稍后必须检查null。
Or you can do *num->cell = myBlankString;
或者你可以做
*num->cell = myBlankString;
, where blank string is defined as ,其中空白字符串定义为
char myBlankString[1] = "";
This is a printable, blank string. 这是可打印的空白字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.