#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct contact {
char name[30];
int phone_number;
char address [30];
};
int main(int argc, char **argv) {
struct contact friend;
strcpy(friend.name, "Jane Doe");
friend.phone_number = 377177377;
strcpy(friend.address, "3771 University Avenue");
char *name;
int number;
char *address;
update_contact(&friend, name, number, address);
return 0;
}
我需要实现update_contact
函数来更新联系信息。 friend
和number
是普通变量。 *name
和*address
是指针。但是函数调用也使用指针friend
的地址,即&friend
。现在,我对应该在函数参数中输入什么感到非常困惑。
我试图放入指针。
void update_contact (struct *c, char *name, int number, char *address) {
c->name = name;
c->phone_number = number;
c->address = address;
}
但是,这会带来很多错误,例如
error: request for member 'address' in something not a structure or union
&c->address = address;
我该如何解决?谢谢
这是固定版本。谢谢托马斯·贾格和一些名字。
void update_contact (struct contact *c, char *name, int number, char *address) {
strcpy(c->name, name);
c->phone_number = number;
strcpy(c->address, address);
}
答案 0 :(得分:1)
如David C. Rankin所建议:
您需要初始化作为参数传递的变量。 例如
function buildArray(a, b) {
var arr = [];
for (var i = a; i <= b; i++) {
arr.push(i);
}
return arr;
}
console.log(buildArray(5, 10));
char *name = "Donald Trump";
int number = 01010101010;
char *address = "White House;
或者您可以在调用函数时使用文字和常量
char name[] = "Donald Trump";
int number = 01010101010;
char address[] = "White House;
update_contact(&friend, "Donald Trump", 0101010010, "White House");
答案 1 :(得分:1)
在功能update_contract
中,
struct
替换为struct contact
strcpy
如下
void update_contact(struct contact *c, char *name, int number, char *address) {
strcpy(c->name, name);
c->phone_number = number;
strcpy(c->address, address);
}