0

I am learning graph (Adjacency List) from geeks to geeks and I see this code:

// A structure to represent an adjacency list node
struct AdjListNode
{
    int dest;
    struct AdjListNode* next;
};

// A structure to represent an adjacency liat
struct AdjList
{
    struct AdjListNode *head;  // pointer to head node of list
};

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
    int V;
    struct AdjList* array;
};

I would like to know the difference betwee:

struct AdjListNode* next; and struct AdjListNode *head; and struct AdjListNode * head;

Pat R Ellery
  • 1,696
  • 3
  • 22
  • 40
  • 3
    Nothing but style. syntactically they are are all the same. – hoss May 22 '15 at 15:50
  • The whitespace is insignificant here. You might want to have a look at http://cdecl.org/ – shuttle87 May 22 '15 at 15:54
  • The `*` is always associated with the *declarator*, not the type specifier; `T *a` and `T* a` are both parsed as `T (*a)`. Whitespace makes no difference in this case. – John Bode May 22 '15 at 17:02

4 Answers4

4

They are the same, only different styles of writing "a pointer to a struct AdjListNode".

Jeff Ames
  • 1,424
  • 10
  • 18
2

The answer to this question is they are the same. Even though they are all the same it is quite interesting to why actually they are the same.

it is because according to C standard 6.4.6 Punctuators the character "*" is punctuator. Therefore it can not be a part of a name of type.

As you can read in 6.4.6.2 A punctuator is a symbol that has independent syntactic and semantic significance.

This is why it is treated differently to the ordinary character which makes a type name, therefore being interpreted as a pointer. Because it can't be a part of a name the compiler always assume you ended the name of the type as soon as it will see the "*", for the case where space is first the white spaces are simply ignored.

The section 5.1.1.2 - Translation phases of standard describes how the white-spaces are treated by compiler. (and in fact that after tokenisation they are no longer important).

I thought it may be worth adding.

cerkiewny
  • 2,761
  • 18
  • 36
1

As others already said, it's the same thing, however, take a look at the following example:

int main() {
    int* a = NULL , b;
    b = 5;
    a = &b;
    printf("a = %p, *a = %d, b = %d", a,  *a, b);
    return 0;
}

output: a = 000000000022FE44, *a = 5, b = 5 in the first line int* a = 0 , b; only a is of type int*(aka pointer) in other words, the * is relevant only for the first variable, this means that b is of type int

ThunderWiring
  • 738
  • 1
  • 7
  • 29
1

Indeed it is just a style, same as writing a+b vs a + b vs a+ b.

However, I'd like to point out that it is common in C to write the star next to the variable name, like this:

int *p;

Main reason for this is that you know that p is a pointer. Especially if you declare multiple variables in a single line, like this:

int* pointer, not_a_pointer, *another_pointer;

In contrast, in C++, it is common to put the asterisk next to the type, as it is info about the type and not a variable: int* p

To summarize:

  • In C, the variable is usually read as a pointer to type int.
  • In C++, the variable is usually read as of type "pointer-to-int"

Hope this makes sense.

mtijanic
  • 2,872
  • 11
  • 26