c - 使用指针时类型不兼容

标签 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;
}

我收到以下错误:

error: incompatible type for argument 1 of 'print_contact'
note: expected 'struct contact *' but argument is of type 'contact'.

我已经在别处声明了 my_string 结构,我认为这不是问题所在。我只是不确定如何让打印过程调用和过程声明具有匹配的类型。

最佳答案

您的编译器告诉您将指针类型传递给 print_contact 函数,如下所示:

print_contact(&contacts.data[i]);

关于c - 使用指针时类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30591049/

相关文章:

CustomAction 在开发计算机上成功,在部署计算机上失败

c - C 中的指针 (while(*s1++ = *s2++))

java - 不兼容的类型,但没有意义(Java)

c 程序未按预期输出

C计算日期之间的差异

c++ - 成员指针在函数返回时被覆盖?

c++ - 错误 : incompatible types in assignment of 'char*' to 'char [4000]'

java - 类列表 <?扩展 Foo> 不兼容的类型

c - 我们如何使用可变变量作为开关盒标签

C 为什么将数组的首地址传递给 char 指针会提供整个字符串?