开发者

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.yearOfManufact开发者_StackOverflowure = 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 ?


Forward declaration should be :

void PrintCarDetails(Car * details); 


void PrintCarDetails(Car *details); 

*
is missing in the forward declaration.


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.


You probably misspinted declaration of PrintCarDetails function. Should be:

void PrintCarDetails(Car *details);

works here


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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜