1

Mahlzeit community,

i've a new question :D

Does it makes any difference where i write the * when i wanna declarate a pointer?

E.g.

void loopsWithCStringsV2()
{
    const char*  str1 = "i love learning";
    const char * str2 = "i love learning";
    const char  *str3 = "i love learning";
    
    std::cout << "str1: " << str1 << std::endl;
    std::cout << "str2: " << str2 << std::endl;
    std::cout << "str3: " << str3 << std::endl;

    std::cout << "&str1: " << &str1 << std::endl;
    std::cout << "&str2: " << &str2 << std::endl;
    std::cout << "&str3: " << &str3 << std::endl;

    std::cout << "*str1: " << *str1 << std::endl;
    std::cout << "*str2: " << *str2 << std::endl;
    std::cout << "*str3: " << *str3 << std::endl;
}

will result in:

str1: i love learning
str2: i love learning
str3: i love learning
&str1: 006FFAA8
&str2: 006FFA9C
&str3: 006FFA90
*str1: i
*str2: i
*str3: i

Enjoy your day :)

  • 3
    No. White spaces are mostly ignored – Benny K Aug 27 '20 at 10:53
  • 1
    No it doesn't. Formally it's `const char *str1;` although it's been the fashion for years to regard it as `const char* str1`. – Bathsheba Aug 27 '20 at 10:53
  • 1
    `*` is a token that doesn't require whitespace, so it doesn't matter. You can even do abomination like `const char*str4`. – Yksisarvinen Aug 27 '20 at 10:54
  • 1
    @Yksisarvinen: I had a quant who insisted on doing that. Now works in model validation. – Bathsheba Aug 27 '20 at 10:55
  • You can even write `const`, `char`, `*`, and e.g. `str1` all on different lines (as well as the `=` and the string literal). – Some programmer dude Aug 27 '20 at 10:55
  • 2
    there are good points for either: The `*` is part of the type not of the name, hence `int* x;` on the other hand, actually the grammer rules say that `*` belongs to `x` not to `int` which matters if you have more than one in a line, hence `int *x,*y;`. I am sure there are duplicates... – 463035818_is_not_an_ai Aug 27 '20 at 10:55
  • @idclev463035818: There are dupes out there for this one, hence nobody dares answer this. – Bathsheba Aug 27 '20 at 10:56
  • fwiw, I just checked the [core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) and they don't mention it, though all their examples use the convention of one declaration per line and the convention to put the `*` next to the type – 463035818_is_not_an_ai Aug 27 '20 at 11:01
  • @Yksisarvinen That's meaningless. The only tokens that *do* require whitespace are identifiers and numeric literals. – user207421 Aug 27 '20 at 11:07
  • @idclev463035818 If you consider the meanings of `char *a,b;` and `char *a,*b` you will see that the `*` is associated with the identifier, not the preceding type name. – user207421 Aug 27 '20 at 11:08
  • @MarquisofLorne thats exactly what I wrote, no? – 463035818_is_not_an_ai Aug 27 '20 at 11:09
  • I just discovered that the input field when "closing as duplicate" accepts not just an URL, but also works as a search. – MSalters Aug 27 '20 at 11:09
  • @MSalters it just needs a default filter for the questions tags and i'd be happy – 463035818_is_not_an_ai Aug 27 '20 at 11:12
  • Heyho, @MarquisofLorne so this means that {code}char *a, b;{code} would be a variable a: a pointer to a char b: a char and {code}char *a,*b{code} would be a: a pointer to a char b: a pointer to a char so in other words.. the asterics is associated with the following "word" in the declaration? correct me if i'm wrong.. please ;) – SirRollalot Aug 31 '20 at 19:20

2 Answers2

3

Does it makes any difference where i write the * when i wanna declarate a pointer?

Yes, the placement of * matters. For example, these are wrong:

*const char  str1 = "i love learning";
const* char  str1 = "i love learning";
const char  str1 = "i love learning";*

const char*  str1 = "i love learning";
const char * str2 = "i love learning";
const char  *str3 = "i love learning";

Whitespace around * has no effect. In most cases whitespace has little significance in C++, except to separate tokens such as keywords from other text. All of the above are the same.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

The short answer is No.
In your example it doesn't make any difference.
Moreover, you can write something like this, and it will behave the same way:

const char*str1 = "i love learning";

BUT actually there is another case when the location of the "*" makes a huge difference.
It's when you write the pointer * before the char and after the char.
e.g. const * char str1 vs const char * str1
In one case you'd get a constant pointer to a char, and in another case you'd get a pointer to a constant char.
For more details check this:
What is the difference between const int*, const int * const, and int const *?

Just Shadow
  • 10,860
  • 6
  • 57
  • 75