0

What's the difference semantically and technically between doing struct user* bob and struct user *bob?

According to some sources it's called thing* bob or thing** bob, which makes sense because it is of type struct* (or pointer to struct)

On the other hand we can also do thing *bob or thing **bob

Take the example program:

struct user
{
int age;
int *p_age2;
};

int main()
{
struct user bob;
struct user* p_bob = &bob;

I am not sure if it's purely a matter of coding style or if there is actually a slight difference somewhere. I'd be interested to hear why one or the other could make more sense. Another important thing to note is that struct tags and struct variables can have no type prior to them sometimes so maybe user* would make more sense then.

ketchup
  • 3
  • 3
  • It's the same thing. – BluesSolo Dec 04 '18 at 16:16
  • Both forms are parsed as `struct user (*bob);`. In C (and C++), the `*` operator in a declaration always binds to the *declarator* rather than the type specifier. Whitespace is not significant in this case. As a matter of style, us older C farts prefer `T *p;` (because it more accurately reflects the syntax and is less confusing when declaring multiple items at once), whereas C++ programmers prefer `T* p;` (because it emphasizes the fact that `p` has pointer type). – John Bode Dec 04 '18 at 18:16

1 Answers1

3

What's the difference semantically and technically between doing struct user* bob and struct user *bob?

To the compiler, none, though the latter syntax is more clear, since the line struct user *bob, alice; will declare bob as a pointer to struct user and alice as a struct user.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 1
    If you're going to declare different types of variable in the same line of code, you arguably have no grounds to be talking about what's clear. – Tim Randall Dec 04 '18 at 16:35