gpt4 book ai didi

c - 从结构类型中删除元素

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:13 25 4
gpt4 key购买 nike

我正在尝试创建此函数以从指针结构数组类型中删除用户选择的元素。

这是我的功能。当我的代码执行此函数时,我不断收到此错误。

error: incompatible types when assigning to type ‘char[1000]’ from type ‘char *’

      PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
void delete_record(pb *PhoneBook)
{
int x;
int iRecord = 0;

print(PhoneBook);
printf("\nEnter number of record you want to delete: ");
scanf("%d", &iRecord);

printf("\nRecord to be deleted: %d. %s\n", iRecord - 1, PhoneBook[iRecord - 1].cFirstName);
for (x = strlen(PhoneBook[iRecord - 1].cFirstName); x <= strlen(PhoneBook[iRecord - 1].cFirstName); x--) {
PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
}
printf("\nModified record: %s\n\n",PhoneBook[iRecord - 1].cFirstName);
}

这是完整的代码。

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

typedef struct phonebook {
char cFirstName[1000];
char cLastName[1000];
char cNumber[1000];
} pb;

int entry(pb *);
void modify(pb *);
void delete_record(pb *);
void print(pb *);
void convert_u(char *);

//global variable
int MAX_NAME_ENTRY = 0;

int main()
{
int iResponse = 0;
pb *PhoneBook = (pb *) calloc(0, sizeof(pb));

if (PhoneBook == NULL) {
printf("\nMemory allocation failed.\n\n");
return 1;
}

do {
printf("\nPhonebook Menu\n****************\n\n");
printf("1. Enter new contact\n2. Modify existing contact\n3. Delete contact\n4. Print Phonebook \n5. Exit\n\n");
printf("Please make selection: ");
scanf("%d", &iResponse);

if (iResponse == 1) {
entry(PhoneBook);
}
else if (iResponse == 2) {
modify(PhoneBook);
}
else if (iResponse == 3) {
delete_record(PhoneBook);
//printf("\nWorking on it...\n");
}
else if (iResponse == 4) {
print(PhoneBook);
}
} while (iResponse != 5);

free(PhoneBook);
return 0;
}

int entry(pb *PhoneBook)
{
int x;
char yes_no[] = "YES";
pb *newPhoneBook = realloc(PhoneBook, (10 * sizeof(pb)));

if (newPhoneBook == NULL) {
printf("\nOut of memory!\n\n");
return 1;
}
else {
PhoneBook = newPhoneBook;
}
if (MAX_NAME_ENTRY == 10) {
printf("\nMax Number of names entered.\n");
}

for (x = MAX_NAME_ENTRY; x < 10; x++) {
if (MAX_NAME_ENTRY == 9) {
printf("\nLast entry.\n");
}
if (x > 0) {
system("clear");
printf("\nAnother entry(yes/no)? ");
scanf("%s", yes_no);
convert_u(yes_no);
}

if (strcmp(yes_no, "YES") == 0) {
printf("\nFirst Name: ");
scanf("%s", PhoneBook[x].cFirstName);

printf("\nLast Name: ");
scanf("%s", PhoneBook[x].cLastName);

printf("\nPhone Number: ");
scanf("%s", PhoneBook[x].cNumber);
MAX_NAME_ENTRY++;
}
else if (strcmp(yes_no, "NO") == 0) {
break;
}
}
}

void modify(pb *PhoneBook)
{
int iModify = 0;
char name_num[6] = {'\0'};
print(PhoneBook);
printf("\nWhich entry would you like to modify? ");
scanf("%d", &iModify);

printf("\nModify name or number? ");
scanf("%s", name_num);
convert_u(name_num);
if (strcmp(name_num, "NAME") == 0) {
printf("\nEnter new name: ");
scanf("%s %s", PhoneBook[iModify - 1].cFirstName, PhoneBook[iModify - 1].cLastName);
}
else if (strcmp(name_num, "NUMBER") == 0) {
printf("\nEnter new number: ");
scanf("%s", PhoneBook[iModify - 1].cNumber);
}
}

void delete_record(pb *PhoneBook)
{
int x;
int iRecord = 0;

print(PhoneBook);
printf("\nEnter number of record you want to delete: ");
scanf("%d", &iRecord);

printf("\nRecord to be deleted: %d. %s\n", iRecord - 1, PhoneBook[iRecord - 1].cFirstName);
for (x = strlen(PhoneBook[iRecord - 1].cFirstName); x <= strlen(PhoneBook[iRecord - 1].cFirstName); x--) {
PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
}
printf("\nModified record: %s\n\n",PhoneBook[iRecord - 1].cFirstName);
}

void print(pb *PhoneBook)
{
int x;

for (x = 0; x < 10; x++) {
if (strlen(PhoneBook[x].cFirstName) == 0 && strlen(PhoneBook[x].cLastName) == 0) {
break;
}
printf("\n%d. Name: %s %s", x + 1, PhoneBook[x].cFirstName, PhoneBook[x].cLastName);
printf("\n Number: %s\n\n", PhoneBook[x].cNumber);
}
}
void convert_u(char *string)
{
int x;

for (x = 0; x < strlen(string); x++) {
string[x] = toupper(string[x]);
}
}

对我做错了什么有什么建议吗?

最佳答案

数组名不是可修改的左值。因此,数组在 C不可赋值

引用 C11,章节 §6.5.16

An assignment operator shall have a modifiable lvalue as its left operand.

关于可修改的左值,章节 §6.3.2.1

A modifiable lvalue is an lvalue that does not have array type, [...]

您需要使用 strcpy()而不是复制内容。

关于c - 从结构类型中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com