is their any body tell me the Difference between int* a; and int *a; ?

Dani AI

Generated

Short answer: there is no semantic difference for a single declarator; both forms declare a pointer to int. Spacing is ignored by the compiler, so is right that this is largely style. The important bit shows up when you declare more than one name at once.

Key rule: the * binds to the declarator (the variable), not the type. That is why and warn that comma-separated declarations can surprise you. To keep bugs out of reviews, prefer one name per declaration and keep the * visually with the name you are declaring. Compilers do not care; humans do.

Two quick patterns that make the binding obvious:

int *ap[3];      // ap is an array of 3 pointers to int
int (*pa)[3];    // pa is a pointer to an array of 3 int

int (*fp)(double);  // fp is a pointer to function(double) -> int
int* fr(double);    // fr is a function(double) returning int*

Modern C++ tips that scale well:

  • Use one-declarator-per-line and initialize immediately.
  • Consider a type alias so the pointer-ness travels with the alias:
    using IntPtr = int*;
    IntPtr u{}, v{};          // both are int*
    const IntPtr cp = nullptr;  // cp is int* const (const pointer to int)
    using ConstIntPtr = int const*;  // pointer to const int
  • Be careful with auto in multi-declarator lines; auto* r = &n, s = &n; is confusing and often ill-formed. Prefer separate auto declarations instead.

If you follow those habits, it will not matter whether you write int* a; or int *a; in single-name declarations, and you will avoid the pitfalls that prompted this thread.

Recommended Answers

All 8 Replies

I don't believe there is a difference. I think it's just a matter of coding standards.

Yep, almost all spaces (except those between keywords) are ignorable, so in the end the compiler would just see it as

int*a

anyway.

>is their any body tell me the Difference between int* a; and int *a; ?
From a language standpoint there's no difference. However, placing the asterisk by the variable makes more sense when you have multiple variables in a declaration:

int *p, *q;

The reason this makes more sense is because when the asterisk is paired with the type, it's easy to forget that it only applies to the first variable:

int* p, q; // q should be a pointer, but it's not

And of course, when you fix that error, it just looks funny:

int* p, *q;

If you only have a single variable per declaration, it's not an issue and you can do whatever you think makes more sense. The problems only show up with multiple variables per declaration.

thank you very much

wat happens if there is more than one variable

dont matter. same thing its like sayin a*b versus a * b....its the same thing except for where the space is. the compiler wont care for a space in that situation

commented: If you're going to post in a dead thread, at least post something correct. +0

dont matter. same thing its like sayin a*b versus a * b....its the same thing except for where the space is. the compiler wont care for a space in that situation

This is relevant to the OP and the previous post's questions how???
This:

int* a, b;

Is not the same as this:

int *a, *b;

The first one produces an integer pointer called "a" and an integer called "b". It is a confusing and misleading formatting style that makes it look like the '*' extends to "b" when, in actuality, it does not.

The second produces 2 integer pointers called "a" and "b". The '*' operator associates with the variable name, not the datatype. It's area of effect does not extend past the first comma operator (',') to the right.

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.