0

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

satnam
  • 1,457
  • 4
  • 23
  • 43
  • And what about the difference between `struct node *head` and struct node `struct node *head` ???? – ta.speot.is Sep 20 '14 at 21:56
  • 1
    What is the difference between `a= a+b` and `a =a+b` ? – dari Sep 20 '14 at 21:56
  • There is no difference whatsoever between `struct node* head` and `struct node * head` and `struct node *head` and `struct node *head`. – T.C. Sep 20 '14 at 21:56
  • The two code blocks have the *exact* same meaning to the compiler. It's a matter of taste which one to use. – 5gon12eder Sep 20 '14 at 21:57
  • 1
    SO is actually [full](https://stackoverflow.com/questions/9280265/how-to-declare-a-pointer-in-c-syntax) [of](https://stackoverflow.com/questions/2367202/c-pointer-in-function) [duplicates](https://stackoverflow.com/questions/12626929/pointer-c-declaration) of this question. – 5gon12eder Sep 20 '14 at 22:01

2 Answers2

4

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
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
1

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.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60