Is there any difference between int * and Type *?

I queue in C. I have a structure like:

struct Node {
    Node* next;
    Node* previous;
    // data
}

Because nextand previousare just pointers, whether the value of importance is, if I use Node*or int*? eg:

struct Node {
    int* next;
    int* previous;
    // data
}
+3
source share
6 answers

Use int *is incorrect and leads to undefined behavior. You can use either Node *, or void *, but if you are using a type other than Node *, it will require re-translation (or implicit conversion, for example, through an assignment), before Node *you can dereference it.

+5
source

( , ), , . 1 Node * sizeof (Node) , == sizeof (int).

, , ( ), .

+8

, . Node int (Node , int), int Node .

+4

.

, , , , , C .

.

, , casted-type .

, C, ( ), (++ C).
struct Node {
    struct Node* next;
    struct Node* previous;
    // data };
+2

int *, , , Node *, , node.

The difference is the size of the pointed space with a pointer. When it is int *, the ++ pointer shifts the sizeof (Node) pointer, and with its Node *, the ++ pointer shifts the sizeof (Node) pointer.

0
source

If there is

Node * pNode;

the expectation is to be able to write expressions like the following without any cast

(*pNode).next = ...
pNode->next = ...
0
source

Source: https://habr.com/ru/post/1765920/


All Articles