Reputation: 1074
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
Reputation: 1
It is just a little mistake, your function definition and declaration don't match:
void PrintCarDetails(Car details);
void PrintCarDetails(Car *details);
just fix the line 12 with : void PrintCarDetails(Car *details);
Upvotes: 0
Reputation: 5609
You probably misspinted declaration of PrintCarDetails function. Should be:
void PrintCarDetails(Car *details);
works here
Upvotes: 3
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
Reputation: 43311
void PrintCarDetails(Car *details);
*is missing in the forward declaration.
Upvotes: 3
Reputation: 300499
Forward declaration should be :
void PrintCarDetails(Car * details);
Upvotes: 6