我们必须如何声明一个结构?

问题描述 投票:1回答:2
typedef struct cellule
{
    int numero;
    int poids;
    struct cellule *suivant;
}Celulle, *LISTE;  



LISTE graphe[TAILLE];

我不明白*LISTE是什么意思?

c
2个回答
0
投票

有两种可接受的方法来声明此结构:

typedef struct cellule
{
    int numero;
    int poids;
    struct cellule *suivant;
}Celulle;

// usage:
Celulle c;

struct cellule
{
    int numero;
    int poids;
    struct cellule *suivant;
};

// usage:
struct celulle c;

选择哪个是编码风格的问题。到目前为止,前者是最常见的。后者用于Linux风格的程序。都可以。

然而,永远不能将指针隐藏在typedef后面。您的原始代码执行此操作,定义了类型LISTE,该类型实际上是Celulle*指针。像这样隐藏指针会造成混乱,并因此而导致错误。相反,您的原始程序应重写为如下形式:

typedef struct cellule
{
    int numero;
    int poids;
    struct cellule* suivant;
}Celulle;

Celulle* graphe[TAILLE];

3
投票

已经有很多有关此的信息。

基本上是指向您定义的结构的指针的别名。

typedef struct cellule
{
    int numero;
    int poids;
    struct cellule *suivant;
}Celulle, *LISTE;

可以分割为:

struct cellule
{
    int numero;
    int poids;
    struct cellule *suivant;
};

typedef struct cellule Celulle;
typedef struct cellule * LISTE;

使LISTE成为该结构的Poniter的别名。

*CelulleLISTE是相同的类型。

LISTE graphe[TAILLE];

您正在声明该结构的指针数组(大小为TAILLE)。

© www.soinside.com 2019 - 2024. All rights reserved.