pointer inside struct

Hi guys, can someone explain me the difference in meaning between
1
2
3
4
5
6
struct AdjListNode 
{ 
    int dest; 
    int weight; 
    struct AdjListNode* next; 
}; 

and
1
2
3
4
5
6
7
 
struct AdjListNode 
{ 
    int dest; 
    int weight; 
    struct AdjListNode *next; 
}; 
Absolutely nothing, it's just a formatting preference.
But why do they use 'struct AdjListNode*' in the definition of this function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest, int weight) 
{ 
    struct AdjListNode* newNode = 
            (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 
    newNode->dest = dest; 
    newNode->weight = weight; 
    newNode->next = NULL; 
    return newNode; 
} 




Because the function returns a pointer to an AdjListNode.
can someone explain me the difference in meaning between

To your compiler, no difference. The different whitespace formatting is for us silly humans.

There are 3 different whitespace options regarding pointer declaration:

int* foo;, int *foo; & int * foo;.

I personally prefer the first option. Because a "pointer to an int" is NOT an int.

int* foo; is not int foo;. Totally different types.

Whether I am declaring a pointer to a variable or a function that returns a pointer I always put the * next to the type. It makes it obvious (to me) it is a different type.

Since this is a question about whitespace formatting, readability, others will have their own "religious doctrine" as to the Only Way To Format. And defend their choices as it were a Holy War.

I'm usually agnostic/atheistic on The One True Way. If I "borrow" code for my own uses that uses a different formatting I'll reformat to "my style," unless the project has an already established format and I'm submitting changes back to the shared code base.
Since this is a question about whitespace formatting, readability, others will have their own "religious doctrine" as to the Only Way To Format. And defend their choices as it were a Holy War.

... and the overwhelming majority of developers will be reasonable and mature about it, understanding that that there are valid reasons to prefer either style, and that it's mostly a matter of taste.

I know it's gratifying to imagine onesself as the lone voice of reason in an ocean of insanity, but let's not get carried away here.
@MikeyBoy, try reading the latter two paragraphs of my comment with your tongue FIRMLY jammed into your cheek. Sarcasm works wonders.
Remember that no-one but you can hear the tone of voice you imagined yourself saying those words in.
GillesCplusplus (8)
this is a struct that contains a pointer to itself, which is used for linked list or tree type data structures. You can't contain yourself directly; the compiler does not know when to stop... a thing has a thing which has a thing which has a thing.. its like looking into a pair of mirrors. But the pointer is not making the chain automatically, you have to allocate each one. so you can have thing points to thing points to thing points to nothing without the infinite chaining problem. the first one you create is a thing that has a pointer to nothing, and you can start the chain yourself and set it up.

the function you showed is a part of a bigger pile of code that defines some sort of linked list type construct. The function you showed makes a node that is ready to be placed into such a list.
Last edited on
jonnin wrote:
this is a struct that contains a pointer to itself

To be more precise - it contains a pointer to some object of the same type as itself.

In a given instance of AdjListNode, that pointer could point to the same instance, but more likely, it will be set to point to a different instance - as jonnin says, probably in a linked list, or something similar.
Last edited on
There are 3 different whitespace options regarding pointer declaration:

int* foo;, int *foo; & int * foo;

I personally prefer the first option. Because a "pointer to an int" is NOT an int.


But remember that that first method can sometimes seem confusing to beginners.

int* foo, bar;

What type of variable is bar?

By the way I'm not disagreeing with you about your preferences, and I do know that this question leads to the debate about having having/not having multiple declarations on the same line.

int* foo, bar; If we are going off on our soapboxes, I would say never write this (it effectively mixes 2 types on the same line, which is not obvious, beginner or expert, you can miss these too easily). I never do this, but you do C it in some code. Where the * is aside, its the mixing 2 types on line like that I am frowning at.
int *foo, bar; //just as bad to me.

Last edited on
@jlb, that is why I personally NEVER declare more than one variable at a time. I was bit too many times by single line/multiple declarations.

And will SUGGEST to others to refrain from doing so as well, stating why they should not. Forewarned is forearmed in stopping a lot of simple, silly mistakes.

What type of variable is bar?

POD int, duh. :Þ

Making simple mistakes like you point out, repeatedly, reinforced for me trying to save a few keystrokes most times isn't worth it.

If others choose a differing formatting, fine by me. "Religious coding wars" are neither fun, nor are they constructive in the long run.
Topic archived. No new replies allowed.