Luca
Luca

Reputation: 55

Typedef struct pointer to another typedef struct

I have some problems with this code:

typedef struct Product {

    char product_code[5];
    int sells;
    int sells_quantity;
}p[3];

typedef struct Seller {
    char seller_code[5];
    Product *ptr;
}seller[5];

Why does it give me an error for Product *ptr?

Upvotes: 0

Views: 232

Answers (2)

Anastasis
Anastasis

Reputation: 194

You can do this :

typedef struct Product {

char product_code[5];
int sells;
int sells_quantity;
}p[3],Product;

typedef struct Seller {
  char seller_code[5];
  Product *ptr;
}seller[5];

Upvotes: 0

Kaizhe Huang
Kaizhe Huang

Reputation: 1006

Could you try replace your code

typedef struct Product {

    char product_code[5];
    int sells;
    int sells_quantity;
}p[3];

With

typedef struct Product {

    char product_code[5];
    int sells;
    int sells_quantity;
} Product;               // from here the structure type Product is recognized by the compiler. 

Upvotes: 1

Related Questions