简体   繁体   English

无法使此数据结构正常工作(新手)

[英]Unable to get this data structure to work (newbie)

I'm pretty new to C, and I've been trying to make a program with this data structure: 我对C很陌生,并且一直在尝试使用这种数据结构编写程序:

struct node {
    char command[100];
    char prereq[100][80];
    char *targ;
    int isUpdated;
} Node;

However, whenever I try to implement it in the code, like this: 但是,每当我尝试在代码中实现它时,就像这样:

Node n = malloc(sizeof(*n));

I get compiler errors such as "nmake.c:13:31: error: unknown type name 'Node'". 我收到诸如“ nmake.c:13:31:error:unknown type name'Node'”之类的编译器错误。

I've tried code such as: 我已经尝试过以下代码:

typedef struct node *Node;

with similar results. 结果相似。 What's wrong? 怎么了?

I think you are confused about what typedef does. 我认为您对typedef功能感到困惑。

A declaration such as this one: 这样的声明:

struct node { ... };

will define a type named struct node . 将定义一个名为struct node的类型。 Do not forget the word struct ! 不要忘记struct一词! If you say just node it will mean nothing (note that this is different in C++). 如果您仅说node ,将毫无意义(请注意,这在C ++中是不同的)。

So now to create a local variable you do: 现在,要创建一个局部变量,您可以执行以下操作:

struct node x;

And to do a dynamic one: 并做一个动态的:

struct node *px = malloc(sizeof(struct node));

Now, if you want to make an alias to the type, you use the same exact syntax than to create a variable, but you write typedef at the beginning of the declaration: 现在,如果要为类型创建别名,则使用与创建变量相同的语法,但是在声明的开头写入typedef

typedef struct node Node;

Or if you prefer the pointer type: 或者,如果您更喜欢指针类型:

typedef struct node *PNode;

Then to create variables: 然后创建变量:

Node a;
Node *b = malloc(sizeof(Node));
PNode c = malloc(sizeof(Node)); //do not use sizeof(PNode) as it is useless here

Some people, as @WhozCraig says in the comments, and you seem to intend in the original code prefer to use the declaring variable for sizeof instead of the type: 就像@WhozCraig在评论中所说的那样,有些人似乎在原代码中打算使用对sizeof声明的变量,而不是类型:

Node *b = malloc(sizeof(*b));
PNode c = malloc(sizeof(*c));

Actually there is no real difference, other than the style, so pick your favorite! 实际上,除了样式之外,没有任何实际区别,因此请选择您喜欢的!

Note that when defining the original struct you can use the same declaration to create variables or typedefs, but not both: 请注意,在定义原始结构时,您可以使用相同的声明来创建变量或typedef,但不能同时使用两者:

struct node { ... } a; //a is a variable

typedef struct node {...} b; //b is a type

Some people like to force the syntax and do something like this: 有些人喜欢强制语法并执行以下操作:

typedef struct node {...} Node, *PNode; //tree types in one line!

but personally I find this practice a bit dumb: don't typedef a pointer unless you want to hide the fact that it is a pointer. 但是我个人认为这种做法有点愚蠢:除非您想隐藏它是指针的事实,否则请不要typedef指针。

What you probably want is: 您可能想要的是:

typedef struct _node {
    char command[100];
    char prereq[100][80];
    char *targ;
    int isUpdated;
} Node;

/*
 * This defines a structure named _node.
 * To use it, you must add struct everytime, for instance:
 * struct _node foo;
 *
 * It also defines a typedef named Node which is a synonym to struct _node - Node
 */ 

Node *n = malloc(sizeof(*n));
/*
 * Because we want to dynamically allocate we need a pointer, so we declared it as a Node *
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM