0

I am learning at the moment about pointers and lists. I had one question about the typedef keyword.

typedef int data;
typedef struct nodo *lista;
struct nodo {
  data el;
  struct nodo *next;
};

If I write down:

lista l;

Is l a pointer or a struct type. I want to know the part typedef struct nodo *lista; defines lista to be a pointer of struct type or a struct?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Never typedef pointers. You are only making it easier to make mistakes that way. Maybe the only real reason to do that is maybe function pointers. – Fredrik Nov 26 '19 at 10:35
  • I would like to know what does the second line of code make. –  Nov 26 '19 at 10:36
  • 1
    `lista l;` is the same thing as `struct nodo *l;`. Now which one is easier to understand? In the first case you don't know if `l` is a pointer or not, in the second case you know. – Jabberwocky Nov 26 '19 at 11:05
  • It's curious that the structure definition doesn't use `lista` in place of `struct nodo *`. Not precisely wrong, but a little unexpected. – Jonathan Leffler Nov 26 '19 at 11:39

3 Answers3

3

After

typedef struct nodo *lista;

lista is another name for struct nodo *.

So lista l; is the same as struct nodo *l;.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

It defines lista as a pointer to a struct nodo, because of the *.

If it were instead defined as:

typedef struct nodo lista;

lista would be just a struct.

legoscia
  • 39,593
  • 22
  • 116
  • 167
0

Remove the keyword typedef and you will get a declaration of a pointer to an obejhct of the type struct nodo.

struct nodo *lista;

When the keyword typedef is used

typedef struct nodo *lista;

then instead of declaring an object of the type struct nodo * you are introducing an alias for the type struct nodo *.

So instead of writing for example

struct nodo *head;

you can write

lista head;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335