I am new to pointers and there is this code for merge sort of linked lists. And here it has declared a dummy node as struct node dummy; and the next node for dummy node is NULL so to set it we use dummy.next = NULL;.
/* Link list node */
struct node
{
int data;
struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b)
{
/* a dummy first node to hang the result on */
struct node dummy;
/* tail points to the last result node */
struct node* tail = &dummy;
/* so tail->next is the place to add new nodes
to the result. */
dummy.next = NULL;
//Code continues...
}
I understand that i can use it if it was struct node *dummy;
but we cant use it here as it is not a pointer node.
So my question is why doesn't dummy->next = NULL work here?
and what is the difference between struct node and struct node* ??
a -> b is shorthand for (*a).b.
If a is not a pointer, *a is not valid, and neither is a -> b.
dummy is not a pointer to a structure. It is the structure variable itself.
You can derefer attributes of a structure with the operator -> only if it is a pointer to the structure.
If you are using the struct variable, then . is the way to go about, which is very much the case with dummy.
I understand that i can use it if it was struct node *dummy;
If by "it" you mean struct node dummy; then the answer is no. You cannot use a pointer to node in the same way as a pointer to node.
So my question is why doesn't dummy->next = NULL work here?
Because dummy is a node, not a pointer, and operator -> if for pointers. The expression dummy->next has the same semantics as (*dummy).next.
. So my question is why doesn't dummy->next = NULL work here? and what is the difference between struct node and struct node* ?
Declared as this struct node dummy;
dummy->next=NULL doesn't work because dummy is not a pointer to struct .
If you write so -
struct node A; // then A is a struct variable which can access struct members using '.' operator
and this -
struct node* B; // then B is a pointer to struct node which can access struct member using '->` or like this (*B).data=something.
Related
I was making a linked list in C, then a query raised in my mind that (read the title above)
struct node
{
int data;
char age;
} temp;
// versus
struct node
{
int data;
struct node* next;
} *temp;
A struct like node cannot contain itself. If this were allowed, each struct node variable would be infinitely sized.
struct node* next; is a pointer to a struct node value. A pointer has a known, finite size and thus a struct can contain a pointer to another value of the same type. By having a pointer to a next struct node you are creating a linked list. This pointer can also be NULL which allows your list to have an end.
In this code a head object is made right in front of the structure definition,
but the n is made in int main.
Can we just declare both the way head was declared? I think I'm just not understanding how struct node *n is different from the way head was declared, because they are both objects right?
struct node
{
int data;
struct node *next;
}*head;
.
.
.
int main()
{
int i,num;
struct node *n;
head = NULL;
...
In your code you are mixing the declaration of struct node and the declaration of the variable head, which is correct C but TBH not very readable.
Write your code rather like that:
struct node
{
int data;
struct node *next;
}; // declaration of struct node
.
.
struct node *head; //declaration of a variable head of type "pointer to struct node"
.
.
.
int main()
{
int i,num;
struct node *n; //declaration of a local variable n of type "pointer to struct node"
head = NULL;
...
It's equivalent to your code, but more readable.
"head" is a global pointer where as "n" is local pointer to node structure.
Also note that neither "head" nor "n" are objects. They are pointer to objects of node structure. You need to allocate memory for them.
I've used typedef before, but I've never used one with a pointer. What effect does this have on the typedef?
Code for reference:
typedef struct node NODE, *PNODE, **PPNODE;
Instead of using struct node you can replace it with NODE
Instead of using struct node* you can replace it with PNODE
Instead of using struct node** you can replace it with PPNODE
The statement can be broken down to
typedef struct node NODE;
typedef struct node* PNODE; // PNODE is pointer to node
typedef struct node** PPNODE; // PPNODE is pointer to pointer to node
The single typedef line defines three type aliases. The second and third are "pointer to node" and "pointer to pointer to node" respectively.
It may be easier to understand if split into three statements:
typedef struct node NODE;
typedef struct node *PNODE; // PNODE is pointer to node
typedef struct node **PPNODE; // PPNODE is pointer to pointer to node
It doesn't affect the typedef, it affects the type.
PNODE is a pointer to struct node
PPNODE is a pointer to a pointer to struct node
In the code
struct link *node
{
int data;
struct link *next;
};
Is next element pointer to pointer?
And what does node=node->next do?
The following isn't valid C (it won't compile):
struct link *node
{
int data;
struct link *next;
};
You probably want:
struct link
{
int data;
struct link *next;
} * node;
Is next element pointer to pointer[?]
No, (if instantiated) next is a pointer to struct link.
what does node=node->next do?
It assigns to node where node->next would point to.
In your struct, the instance field next stores a pointer to the next link.
This line
currNode = currNode->next;
...makes the pointer currNode point to the node that follows the node that currNode previously pointed to.
The operator -> is the same as dot syntax but for pointers.
However, in the code you provided node->next wouldn't do anything because you do not have a variable named "node".
Also, the definition of you struct won't compile. It should be struct link, not struct link *node.
This question already has answers here:
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 8 years ago.
I studying how to create linked lists in C. Take a look at this article.
First he creates the structure using the following code;
struct node
{
int data;
struct node *next;
};
Its clear that *next is a pointer variable of the type node.
But when he goes forward, he does this;
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
Now here I have a problem comprehending what he is trying to do; is he creating nodes of the names, head, second and third? or he is simply trying to create pointer variables of the type node?
Since he puts them equal to NULL; I'd assume he is trying to create pointer variables. But couldn't he do the same using this?
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
Thanks
In C, the whitespace before or after the * is meaningless. So:
struct node *head;
struct node * head;
struct node* head;
struct node*head;
are all exactly the same. C doesn't care about that whitespace.
Where you get into trouble is when you declare multiple items:
struct node *head, tail; // tail is not a pointer!
struct node *head, *tail; // both are pointers now
struct node * head, * tail; // both are still pointers; whitespace doesn't matter
Both are the same technically.....
struct node *third = NULL;
struct node* third = NULL;
does the same thing as the compiler doesn't count the white spaces.