Lew*_*wis 2 c pointers incompatibletypeerror
#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;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: incompatible type for argument 1 of 'print_contact'
note: expected 'struct contact *' but argument is of type 'contact'.
Run Code Online (Sandbox Code Playgroud)
我已经在my_string其他地方宣布了这个结构,我认为这不是问题所在.我只是不确定如何获得打印过程调用和过程声明以匹配类型.
您的编译器告诉您将指针类型传递给print_contact函数,如下所示:
print_contact(&contacts.data[i]);
Run Code Online (Sandbox Code Playgroud)