0

I'm a bit confused as to where to place the * when declaring a new pointer.

For example, let's say I've made a linked list and declared a custom structure node.

I've noticed when I was making speller, I tried both of the following syntax and the program still compiled and worked:

node *head = hashtable[i];
node* head = hashtable[i];

What is the correct way to write it?

1 Answer 1

0

The most important thing is that both forms are correct, the way to write it is just a question of preferences, write it in the way that is most clear to you, that if, you must be consistent with the rest of the code and not write it each time in a way different

2
  • Just to clarify, this also applies to other types of structs too right? i.e. char *word = "hello" would be the same as char* word = "hello"?
    – doctopus
    Commented Mar 20, 2018 at 9:51
  • They are the same, the whitespace is ignored. I'd like to add that char* word better describes the type of word (the * is part of the type, not of the name), but can lead to confusion. char* word, ch; declares two variables, but word is of type char*, while ch is of type char. char *word, ch; would have been less confusing. But usually avoid this kind of variable declaration.
    – Blauelf
    Commented Mar 20, 2018 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .