【发布时间】:2015-06-02 08:31:32
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
typedef struct contact
{
my_string name;
my_string email;
int age;
} contact;
typedef struct contact_array
{
int size;
contact *data;
} contact_array;
void print_contact(contact *to_print)
{
printf("%s (%s) age %i\n", to_print->name.str,
to_print->email.str, to_print->age);
}
int main()
{
int i;
contact_array contacts = { 0, NULL };
for(i = 0; i < contacts.size; i++)
{
print_contact(contacts.data[i]);
}
return 0;
}
我收到以下错误:
error: incompatible type for argument 1 of 'print_contact'
note: expected 'struct contact *' but argument is of type 'contact'.
我已经在别处声明了my_string 结构,我认为这不是问题所在。我只是不确定如何让打印过程调用和过程声明具有匹配的类型。
【问题讨论】:
-
将 void print_contact(contact *to_print) 更改为 void print_contact(contact to_print)。您正在传递的 contacts.data[i] 不是地址,而是数据块本身
-
@Lewis,如果您只是要打印值,则不需要通过引用传递。只需将
void print_contact(contact *to_print)更改为void print_contact(contact to_print)
标签: c pointers incompatibletypeerror