Hi,

Can anyone explain in detail what is the difference between int *a and int* a? Is there any difference between them?

Dani AI

Generated

gave the short correct answer and and highlighted the practical bits: the compiler does not care about the space, and the placement of * interacts with C's declaration grammar. For clarity, the asterisk attaches to each declarator in the declaration, not to the base type; that is the language reason behind the common confusion. See the C declarator grammar for the formal rule: .

Two practical recommendations that help avoid bugs and confusion:

  • Prefer one variable per declaration so the pointer-ness is explicit and cannot be misread. This reduces mistakes when adding more variables on the same line.
  • If a project wants a pointer-to-type visual as part of the type, consider a typedef (or in C11 an alias) so declarations are consistent. Example:
typedef int *IntPtr;
IntPtr p, q;   /* both are pointers to int */

Use that approach with caution: typedefs hide the pointer syntax and can be surprising to readers who expect * next to the name.

Style consistency matters more than choosing one exact spacing convention. Pick a convention (place * next to the declarator or next to the type) and apply it across the codebase, and prefer single-declaration lines in public headers and complex code. For the language-level detail, consult the C standard grammar or the cppreference page above.

Recommended Answers

All 4 Replies

There is no difference -- just programmer preference.

Hi,

Can anyone explain in detail what is the difference between int *a and int* a? Is there any difference between them?

No difference.. But it is better do it 'int *a;' because of readability and is more understandable.

Eg: Suppose the statement is

int* a, b;

it is actually equivalent to

int *a;
int b;

not

int *a;
int *b

If we had written the original statement as

int *a,b;

this would have been more obvious to someone who reads it who is a new bie.

commented: I agree +0

Hey Perry 31,
I recently was reading up on white space rules for C. From what I gathered, tokens such as variable names, function names, variable data type names, other important words in the language, when in contact with operators are naturally separated.

Because of the phenomenon being true that int *var; is syntactically equivalent to int* var;, int*var;, and int<any number spaces>*<any number spaces>var;,I would suffice it to say that the pointer is considered an operator, which would explain why the organization does not and will never have an effect on the functionality when you, I, or anyone else are programming.

Hey Perry 31,
I recently was reading up on white space rules for C. From what I gathered, tokens such as variable names, function names, variable data type names, other important words in the language, when in contact with operators are naturally separated.

Because of the phenomenon being true that int *var; is syntactically equivalent to int* var;, int*var;, and int<any number spaces>*<any number spaces>var;,I would suffice it to say that the pointer is considered an operator, which would explain why the organization does not and will never have an effect on the functionality when you, I, or anyone else are programming.

In otherwords, to boil down that long-winded post, the answer is like I said before, There is no difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.