I am implementing a queue in C. I have a struct like:
我在c中执行一个队列,我有一个结构:
struct Node {
Node* next;
Node* previous;
// data
}
Because next
and previous
are just pointers, does it make a difference if I use Node*
or int*
? eg:
因为next和previous都是指针,如果我使用Node*或int*会有什么不同吗?例如:
struct Node {
int* next;
int* previous;
// data
}
6 个解决方案
#1
5
Using int *
is incorrect and results in undefined behavior. You can use either Node *
or void *
, but if you use any type other than Node *
, it's going to require a cast (or implicit conversion, e.g. via an assignment) back to Node *
before you can dereference it.
使用int *是不正确的,会导致未定义的行为。您可以使用Node *或void *,但是如果您使用节点*之外的任何类型,那么在取消引用之前,它将需要返回到Node *(或隐式转换,例如通过赋值)。
#2
8
Not only does it bring clarity to your code (which is important, the larger your project gets), pointer math only works properly if you're doing the math on the correct type. In your instance, adding 1 to a Node* would result in adding sizeof(Node) to the base pointer, which is almost guaranteed not to be == sizeof(int).
它不仅能使代码更清晰(这很重要,项目的规模越大),指针数学只有在正确的类型上进行计算时才能正常工作。在您的实例中,将1添加到一个节点*会导致将sizeof(节点)添加到基本指针,这几乎保证不等于(int)。
For the example you gave, it wouldn't likely matter (but you'll need a cast).. but do yourself a favor and get in the good habit now of using the correct types.
对于你给出的例子来说,这可能并不重要(但是你需要一个模型)。但是请帮自己一个忙,养成使用正确类型的好习惯。
#3
4
Yes it does. Node
is not an int
(Node
will be larger that int
), so a pointer to int
or a pointer to Node
will make a difference.
的确是的。节点不是int(节点会比int大),所以指向int的指针或指向Node的指针会有所不同。
#4
2
Everybody is right about everything.
每个人都是对的。
The key point is that even though you may know that the internal representation of a pointer is an address, regardless of data type, C makes no such assumption.
关键的一点是,尽管您可能知道指针的内部表示是一个地址,但无论数据类型如何,C都没有这样的假设。
Including an explicit type allows the compiler to do a better job checking your code.
包含显式类型允许编译器更好地检查代码。
Of course, you can always cast the pointer to a different type but then you need to used the casted-type in its proper context.
当然,您总是可以将指针转换为不同的类型,但是您需要在适当的上下文中使用caste -type。
By the way, the strict C I learned (back in the Jurassic age) would not allow your first example (C++ does and so do some modern C compilers). Instead you would have to say
顺便说一下,我学到的严格的C(早在侏罗纪时代)不允许您的第一个例子(c++可以,一些现代的C编译器也可以)。相反,你不得不说
struct Node {
struct Node* next;
struct Node* previous;
// data };
#5
0
When you use int *, You mean that you are pointing to an integer, and when you use Node *, it means you are pointing to a node.
当您使用int *时,您的意思是您正在指向一个整数,当您使用Node *时,它意味着您正在指向一个节点。
The difference is in size of pointed space by pointer. When its int *, pointer++ will shift pointer by sizeof(int) and when its Node *, pointer++ will shift the pointer by sizeof(Node).
不同的是指针指向空间的大小。当它的int *时,指针++将通过sizeof(int)移动指针,当它的节点*时,指针++将通过sizeof(Node)移动指针。
#6
0
If there is
如果有
Node * pNode;
the expectation is to be able to write expressions like following without any cast
期望是能够写出像下面这样的表达式。
(*pNode).next = ...
pNode->next = ...
#1
5
Using int *
is incorrect and results in undefined behavior. You can use either Node *
or void *
, but if you use any type other than Node *
, it's going to require a cast (or implicit conversion, e.g. via an assignment) back to Node *
before you can dereference it.
使用int *是不正确的,会导致未定义的行为。您可以使用Node *或void *,但是如果您使用节点*之外的任何类型,那么在取消引用之前,它将需要返回到Node *(或隐式转换,例如通过赋值)。
#2
8
Not only does it bring clarity to your code (which is important, the larger your project gets), pointer math only works properly if you're doing the math on the correct type. In your instance, adding 1 to a Node* would result in adding sizeof(Node) to the base pointer, which is almost guaranteed not to be == sizeof(int).
它不仅能使代码更清晰(这很重要,项目的规模越大),指针数学只有在正确的类型上进行计算时才能正常工作。在您的实例中,将1添加到一个节点*会导致将sizeof(节点)添加到基本指针,这几乎保证不等于(int)。
For the example you gave, it wouldn't likely matter (but you'll need a cast).. but do yourself a favor and get in the good habit now of using the correct types.
对于你给出的例子来说,这可能并不重要(但是你需要一个模型)。但是请帮自己一个忙,养成使用正确类型的好习惯。
#3
4
Yes it does. Node
is not an int
(Node
will be larger that int
), so a pointer to int
or a pointer to Node
will make a difference.
的确是的。节点不是int(节点会比int大),所以指向int的指针或指向Node的指针会有所不同。
#4
2
Everybody is right about everything.
每个人都是对的。
The key point is that even though you may know that the internal representation of a pointer is an address, regardless of data type, C makes no such assumption.
关键的一点是,尽管您可能知道指针的内部表示是一个地址,但无论数据类型如何,C都没有这样的假设。
Including an explicit type allows the compiler to do a better job checking your code.
包含显式类型允许编译器更好地检查代码。
Of course, you can always cast the pointer to a different type but then you need to used the casted-type in its proper context.
当然,您总是可以将指针转换为不同的类型,但是您需要在适当的上下文中使用caste -type。
By the way, the strict C I learned (back in the Jurassic age) would not allow your first example (C++ does and so do some modern C compilers). Instead you would have to say
顺便说一下,我学到的严格的C(早在侏罗纪时代)不允许您的第一个例子(c++可以,一些现代的C编译器也可以)。相反,你不得不说
struct Node {
struct Node* next;
struct Node* previous;
// data };
#5
0
When you use int *, You mean that you are pointing to an integer, and when you use Node *, it means you are pointing to a node.
当您使用int *时,您的意思是您正在指向一个整数,当您使用Node *时,它意味着您正在指向一个节点。
The difference is in size of pointed space by pointer. When its int *, pointer++ will shift pointer by sizeof(int) and when its Node *, pointer++ will shift the pointer by sizeof(Node).
不同的是指针指向空间的大小。当它的int *时,指针++将通过sizeof(int)移动指针,当它的节点*时,指针++将通过sizeof(Node)移动指针。
#6
0
If there is
如果有
Node * pNode;
the expectation is to be able to write expressions like following without any cast
期望是能够写出像下面这样的表达式。
(*pNode).next = ...
pNode->next = ...