jingo
jingo

Reputation: 1074

Passing structure as pointer

I'm trying to pass structure as pointer in function arguments. Here is my code

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

typedef struct {
    int yearOfManufacture;
    char model[50];
    bool gasoline;
} Car;


void PrintCarDetails(Car details);

int main (int argc, const char * argv[]) 
{        
    Car ford;
    ford.yearOfManufacture = 1997;
    ford.gasoline = true;
    strcpy(ford.model, "Focus");

    PrintCarDetails(&ford);

     return 0;
}

void PrintCarDetails(Car *details)
{
    printf("Car model %s", details->model);
}

I get an error "Passing Car to parameter of incompatible type Car. What I miss ?

Upvotes: 1

Views: 923

Answers (5)

Ranska Jean
Ranska Jean

Reputation: 1

It is just a little mistake, your function definition and declaration don't match:

  • line 12 : void PrintCarDetails(Car details);
  • line 26 : void PrintCarDetails(Car *details);

just fix the line 12 with : void PrintCarDetails(Car *details);

Upvotes: 0

Fedor Skrynnikov
Fedor Skrynnikov

Reputation: 5609

You probably misspinted declaration of PrintCarDetails function. Should be:

void PrintCarDetails(Car *details);

works here

Upvotes: 3

iceaway
iceaway

Reputation: 1264

The function definition differs from the function declaration. In the declaration you state that a a Car struct should be used as an argument, but in the definition you want a pointer to a Car struct.

Upvotes: 3

Alex F
Alex F

Reputation: 43311

void PrintCarDetails(Car *details); 

*
is missing in the forward declaration.

Upvotes: 3

Mitch Wheat
Mitch Wheat

Reputation: 300499

Forward declaration should be :

void PrintCarDetails(Car * details); 

Upvotes: 6

Related Questions