Here is the structure declare code.
这是结构声明代码。
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
And when I write a function like this, the compiler gives a error. It says cur undeclared before first use
.
当我编写这样的函数时,编译器会给出错误。它说在第一次使用之前未被宣布。
bool delete(item* item)
{
assert(item != NULL);
item* cur = NULL;
cur = head;
item* prev = NULL;
while (cur) {
if (cur == item) {
if (prev == NULL) {
head = item->next;
} else {
prev->next = item->next;
}
free(item);
return true;
}
prev = cur;
cur = cur->next;
}
return false;
}
After I look up the reference, it says the typedef
works out just a bit like #define
. It simply makes a substitution at compile time. Is that the reason the code can't be compiled?
在查阅引用之后,它说typedef有点像#define。它只是在编译时进行替换。这是代码无法编译的原因吗?
5 个解决方案
#1
16
In this code:
在这段代码中:
bool delete(item* item)
{
item *cur = NULL;
the item
in the third line is taken to be the name of the variable item
(the parameter to the function), and not the type. Consequently, the third line looks as if it starts out as an expression that multiplies item
by the undefined variable cur
, which leads to problems; the rest of the expression is also bogus.
第三行中的项目被视为变量项目的名称(函数的参数),而不是类型。因此,第三行看起来好像它是一个表达式,它将项目乘以未定义的变量cur,这会导致问题;表达的其余部分也是假的。
If this isn't what you want, don't use the same name for a type and a variable. You'll confuse other people even if you don't confuse yourself and the compiler.
如果这不是您想要的,请不要对类型和变量使用相同的名称。即使你不把自己和编译器混淆,你也会混淆别人。
Whichever reference source said that typedef
and #define
are 'the same' should be dropped from your list of references now! If it can't differentiate two such fundamentally different constructs, it is dangerous because you won't know when it is misleading you (but this is one case where it is misleading you).
无论哪个参考源都说应该从你的参考列表中删除typedef和#define'相同'!如果它无法区分两个这样根本不同的结构,那就很危险,因为你不知道它何时会误导你(但这是一个误导你的情况)。
#2
2
A typedef is just a new name for an already existing type. defines are handled by the preprocessor while typedefs are handled by the C compiler itself. [copied from this link]
typedef只是已存在类型的新名称。定义由预处理器处理,而typedef由C编译器本身处理。 [从此链接复制]
check this question: Are typedef and #define the same in c?
检查这个问题:c中的typedef和#define是否相同?
#3
1
typedef
is not completely same as #define
typedef与#define不完全相同
Here is the difference by example:
以下是示例的区别:
#define cptr1 char*
typedef char* cptr2;
In code:
int main()
{
cptr1 c1,c2; // first case :
// here c1 will be pointer to char but c2 is only char as cptr
// is directly replaced by char* by preprocessor
cptr2 c3,c4; // second case :
// here c3 and c4 both are pointers to char
}
#4
1
you should use -Wshadow
when you compile you code. Then, gcc will tell you where you are wrong. :-) like follows:declaration of ‘item’ shadows a global declaration [-Wshadow]
编译代码时应该使用-Wshadow。然后,gcc会告诉你你哪里错了。 :-)如下:'item'的声明阴影全局声明[-Wshadow]
#5
1
Jonathan Leffler answered you your question.
Jonathan Leffler回答你的问题。
I just want to add: if you write your code in c++, you don't need to typedef, so you can implement your list just like this:
我只想补充一点:如果你用c ++编写代码,你不需要输入type,所以你可以像这样实现你的列表:
struct list_el {
int val;
list_el *next;
};
bool delete_element(list_el *item)
{
list_el *cur = NULL;
...
}
#1
16
In this code:
在这段代码中:
bool delete(item* item)
{
item *cur = NULL;
the item
in the third line is taken to be the name of the variable item
(the parameter to the function), and not the type. Consequently, the third line looks as if it starts out as an expression that multiplies item
by the undefined variable cur
, which leads to problems; the rest of the expression is also bogus.
第三行中的项目被视为变量项目的名称(函数的参数),而不是类型。因此,第三行看起来好像它是一个表达式,它将项目乘以未定义的变量cur,这会导致问题;表达的其余部分也是假的。
If this isn't what you want, don't use the same name for a type and a variable. You'll confuse other people even if you don't confuse yourself and the compiler.
如果这不是您想要的,请不要对类型和变量使用相同的名称。即使你不把自己和编译器混淆,你也会混淆别人。
Whichever reference source said that typedef
and #define
are 'the same' should be dropped from your list of references now! If it can't differentiate two such fundamentally different constructs, it is dangerous because you won't know when it is misleading you (but this is one case where it is misleading you).
无论哪个参考源都说应该从你的参考列表中删除typedef和#define'相同'!如果它无法区分两个这样根本不同的结构,那就很危险,因为你不知道它何时会误导你(但这是一个误导你的情况)。
#2
2
A typedef is just a new name for an already existing type. defines are handled by the preprocessor while typedefs are handled by the C compiler itself. [copied from this link]
typedef只是已存在类型的新名称。定义由预处理器处理,而typedef由C编译器本身处理。 [从此链接复制]
check this question: Are typedef and #define the same in c?
检查这个问题:c中的typedef和#define是否相同?
#3
1
typedef
is not completely same as #define
typedef与#define不完全相同
Here is the difference by example:
以下是示例的区别:
#define cptr1 char*
typedef char* cptr2;
In code:
int main()
{
cptr1 c1,c2; // first case :
// here c1 will be pointer to char but c2 is only char as cptr
// is directly replaced by char* by preprocessor
cptr2 c3,c4; // second case :
// here c3 and c4 both are pointers to char
}
#4
1
you should use -Wshadow
when you compile you code. Then, gcc will tell you where you are wrong. :-) like follows:declaration of ‘item’ shadows a global declaration [-Wshadow]
编译代码时应该使用-Wshadow。然后,gcc会告诉你你哪里错了。 :-)如下:'item'的声明阴影全局声明[-Wshadow]
#5
1
Jonathan Leffler answered you your question.
Jonathan Leffler回答你的问题。
I just want to add: if you write your code in c++, you don't need to typedef, so you can implement your list just like this:
我只想补充一点:如果你用c ++编写代码,你不需要输入type,所以你可以像这样实现你的列表:
struct list_el {
int val;
list_el *next;
};
bool delete_element(list_el *item)
{
list_el *cur = NULL;
...
}