Search code examples
cpointersincompatibletypeerror

incompatible type when using pointers


#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;
}

I am getting the following errors:

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

I have declared the my_string structure elsewhere, and I don't think that that is the problem. I am just unsure as to how to get the print procedure call and the procedure declaration to have matching types.


Solution

  • Your compiler is telling you to pass a pointer type to the print_contact function, like so:

    print_contact(&contacts.data[i]);