Ren
Ren

Reputation: 4683

C++: Declaring pointers

So, in this guide I read that saying

char * terry;

was different from saying

char* terry; //or
char *terry; // FYI: I understand what these two do.

as stated by

"I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign."

However I do not understand why. Perhaps I took the quote the wrong way, now that I have read it once again, but I am still confused. Can anyone tell me if this is wrong or right and why?

Thank you.

Upvotes: 0

Views: 270

Answers (7)

Masa
Masa

Reputation: 311

char* terry; //or char *terry; Simply means you are declaring a pointer of char type.

Now terry hold the address location of a character variable, which you have might asked terry to point.

If you print terry, it will print the memory location.

If you dereference terry ( *terry) you will get the character, it is pointing to.

That is what that write up you are mentioning is telling.

Upvotes: 0

NP Rooski  Z
NP Rooski Z

Reputation: 3657

he is trying to differentiate between the definition char *terry and its usage after the definition e.g. perry = *terry; where perry takes the value of terry rather than the address. As far as compilers are concerned, and others have noted in there answers all three definitions are same. I prefer char *terry, where asterisk(*) hugs the variable.

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224944

No, all three are exactly the same as far as the parser is concerned. People certainly have their reasons for using each style, but there's not really a "right" way. As an editorial note, I prefer:

char *terry;

What the author of your link is describing is that the * in the declaration is somehow different from the unary * operator used to dereference a pointer:

char *terry = "abcdefg"; // declaration & initialization
*terry = 'x';            // dereference

This potential funny business is actually one of the reasons I prefer the notation I mentioned above - it makes both cases look the same, so there's no room for confusion.

Upvotes: 8

Alan
Alan

Reputation: 46823

You may have misread.

char * terry;
char* terry;
char *terry;

Are all the same way to declare a char pointer, named terry.

As for the "right" way, that depends on your coding standard. I personally use char* terry

Upvotes: 0

Aleksandr Dubinsky
Aleksandr Dubinsky

Reputation: 23505

Textbooks often recommend writing char *terry to remind you that in the statement char *terry, jerry terry is a pointer but jerry is not. Avoids a noobie mistake.

Others write char* terry because they read it as "char-pointer terry." These people probably never declare pointers and non-pointers, or even just two pointers, on the same line. Which is not a bad rule to use.

Otherwise, whitespace almost never matters in C++. Except inside identifiers (variable names, function names) and... can't think of anything else.

Upvotes: 1

RussS
RussS

Reputation: 16576

I prefer

char *terry;

Just because then if you do something like

char *terry, *jerry, larry;

It's obvious you forgot a pointer.

Upvotes: 0

shiraz
shiraz

Reputation: 1218

I think all three are the same Once you have declared a pointer e.g.

int *x;

and later on you want to dereference it you'd use

*x

Upvotes: 1

Related Questions