这是两个结构,客户端和雇员
struct client
{
char clID[10];
char cname[50];
char caddress[99];
char cemail[99];
char cfees[6];
char ceID[10];
char cename[50];
}typedef client;
struct employee
{
char empID[10];
char ename[50];
double erate;
double ehours;
double esalary;
int clientCount;
}typedef employee;
这是一个搜索案例,在输入客户信息后,系统会提示用户按ID分配员工,然后将存储在该ID中的员工姓名添加到客户端结构中的中心位置
case 1: getClient(clCount, pcli);
printf("Enter an employee to assign to this client: ");
scanf("%9s", searchID);
searchEmp(searchID, pemp, empCount);
foundAt = searchEmp(searchID, pemp, empCount);
if(foundAt >= 0)
if(pemp[foundAt].clientCount < 5)
{
strcpy(pcli[clCount].ceID, searchID);
此行是错误的来源 pcli [clCount] .cename = pemp [foundAt] .ename;
pcli[clCount].cename = pemp[foundAt].ename;
pemp[foundAt].clientCount++;
}
else
{
printf("Max clients reached for this employee!");
}
else
printf("%s is not found anywhere\n", searchID);
clCount++;
break;
搜索方法:
int searchEmp(char* searchID, employee* pemp, int empCount)
{
int i = 0;
for(i = 0; i < empCount; i++)
{
if(strcmp(searchID,(pemp + i)->empID)==0)
{
return i;
}
}
return -1;
}//end searchClient
答案 0 :(得分:1)
只需使用strcpy
:
strcpy(pcli[clCount].cename, pemp[foundAt].ename);
或strncpy
:
strncpy(pcli[clCount].cename, pemp[foundAt].ename, sizeof(pcli[clCount].cename)-1);
pcli[clCount].cename[sizeof(pcli[clCount].cename)-1] = '\0';
文字赋值在C中不起作用,就像C ++中的字符串一样。它们不会自动复制。他们的地址将被复制,但在这里你甚至使用静态缓冲区......