将typedef与C中的指针一起使用

问题描述 投票:0回答:4

目前我正在学习有关指针和列表的信息。我对typedef关键字有一个疑问。

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

如果我写下来:

lista l;

是指针还是结构类型。我想知道部分typedef struct nodo * lista;将lista定义为结构类型或结构的指针?

c pointers
4个回答
2
投票

之后

typedef struct nodo *lista;

[listastruct nodo *的另一个名称。

所以lista l;struct nodo *l;相同。


1
投票

由于lista,它将struct nodo定义为对*指针

如果改为定义为:

typedef struct nodo lista;

lista只是一个结构。


0
投票

[lista是一个指针,需要像lista->ellista->next一样被取消引用。

我个人更喜欢将*与类型而不是变量名相关联,以使这一点变得清晰:

/* This... */
typedef struct nodo* lista;

/* ...instead of this */
typedef struct nodo *lista;

但这是个人喜好;他们都做同一件事。


0
投票

删除关键字typedef,您将得到一个指向struct nodo类型对象的指针的声明。

struct nodo *lista;

使用关键字typedef时

typedef struct nodo *lista;

然后,您不是声明类型为struct nodo *的对象,而是为类型struct nodo *引入了别名。

因此,而不是例如编写

struct nodo *head;

您可以写

lista head;
© www.soinside.com 2019 - 2024. All rights reserved.