0

I keep getting the following warning: initialization from incompatible pointer type. Through this line

Season season1 = (Season *) malloc(sizeof(Season));

This is the struct I defined in season.h

typedef struct season* Season;
Sam12
  • 1,805
  • 2
  • 15
  • 23
  • 3
    You are assigning a pointer to a Season to a value of type Season. – Nibr Apr 28 '18 at 13:13
  • But I defined Season as a pointer in typedef @Nibr – Sam12 Apr 28 '18 at 13:15
  • 2
    Don't make typedefs of pointers. That leads to erros as you've seen (e.g. now `Season*` is a pointer to a pointer, which is not clear by looking at the code). – interjay Apr 28 '18 at 13:16
  • Actually, the lecturer specified that we define it as a pointer. – Sam12 Apr 28 '18 at 13:18
  • 1
    btw [you don't cast the result of malloc in C](https://stackoverflow.com/q/605845/995714) – phuclv Apr 28 '18 at 13:22
  • Then your lecturer is not up to date on best practices in C. If you must typedef a pointer, at least give it a name that makes it clear such as `SeasonPtr`. – interjay Apr 28 '18 at 15:27

1 Answers1

2

You cast the result from malloc to "pointer to A" and assign it to a variable of type "A". With "A" being "Season".
It might become clearer with this version of your code,
edited for more speaking identifiers and fixed by using the right thing inside sizeof() and not casting the result of malloc().

typedef struct season* PointerToseason; // if you insist on hiding it inside a typedef

PointerToseason season1 = malloc(sizeof(struct season));

A widely preferred version of that is

PointerToseason season1 = malloc(sizeof(*season1));

It requires less knowledge of things which have been hidden inside typedefs (wisely or not).

Also, look closely at the too similar identifiers in your code season and Season.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54