|
-
July 22nd, 2005, 05:29 PM
#1
Best Way To Declare Pointers
Once Again, My Questions is so simple that it's even in the FAQ. My question is, is there a difference between these three statements?
int* i
int * i
int *i
I've seen all three around, and from what I've read I think they're all saying the same thing, but I want to be sure.
Eric
-
July 22nd, 2005, 05:46 PM
#2
Re: Best Way To Declare Pointers
Same thing. The compiler ignores whitespace ( but not in strings, mind you ). So they all mean the same.
Note: if you do this:
int* p,k;
while it appears that both p and k are pointers, it is wrong.
Only p is a pointer, but k is a int
So, people generally stick the * close to the variable and not type. Something like this:
int *p,*k;
-
July 22nd, 2005, 05:48 PM
#3
Re: Best Way To Declare Pointers
 Originally Posted by Carbaholic
Once Again, My Questions is so simple that it's even in the FAQ. My question is, is there a difference between these three statements?
int* i
int * i
int *i
I've seen all three around, and from what I've read I think they're all saying the same thing, but I want to be sure.
Syntactically, no difference at all. They all produce exactly the same code. However, there is a decade-old debate of whether it's clearer to write "int* i" or "int *i".
Personally, I prefer "int* i", since I read this as "declare a variable of the type int* (pointer to an int) with the name i" - and hence, the * is part of the type and should stand next to it. The problem with this notion is the short-hand way of variable declaration in C/C++ which lets you declare a series of variables of the same type, separated by commas, and without repeating the type:
Code:
int a, b, c; // Declares three ints
The problem arises when you write the following:Although this suggests that three pointers are declared, actually, this declares just one pointer (pA) and two ints (pB and pC), since the * is syntactically associated to the variable, not the type. To declare three pointers, you would rather write:
Code:
int *pA, *pB, *pC; // Declares three int pointers
That's why many experienced people prefer putting the pointer operator next to the variable name to clarify this.
However, my personal preference is to put it next to the type, and to completely avoid declaring multiple variables separated by commas (especially if they have mixed levels of indirection). I always write it this way:
Code:
int* pA;
int* pB;
int* pC;
Last edited by gstercken; July 22nd, 2005 at 05:51 PM.
-
July 22nd, 2005, 05:49 PM
#4
Re: Best Way To Declare Pointers
 Originally Posted by kirants
So, people generally stick the * close to the variable and not type.
Generally?
-
July 22nd, 2005, 05:52 PM
#5
Re: Best Way To Declare Pointers
Woops !! We pretty much echoed the same thing 
Well, maybe not generally ... I don't use the int *p1,*p2,*p3 style too..
So, maybe , occasionally is a better word
-
July 22nd, 2005, 07:15 PM
#6
Re: Best Way To Declare Pointers
If we talk about personal preferences, than I always put the * to the Type, when I declare a pointer to Type, and in case there is need for more than just one pointer I declare them separatelly:
Code:
Type* pointer1;
Type* pointer2;
I consider this one to be the most readable form.
-
July 23rd, 2005, 12:34 PM
#7
Re: Best Way To Declare Pointers
 Originally Posted by gstercken
... and hence, the * is part of the type and should stand next to it.
Actually, while this is all a religious issue anyway, in the language itself the "*" actually binds with the variable name. Dan Saks (former secretary of the ANSI and ISO C++ committee and all-round C++ guru) once wrote a multi-part article on declarators in the C++ Users Journal and advocated joining the "*" with the variable name. Funny thing is that Stroustrup himself doesn't do it that way in all of his writings. I personally agree with Dan on this one and have done it that way for 20+ years myself (previously in C). My two cents
-
July 23rd, 2005, 12:59 PM
#8
Re: Best Way To Declare Pointers
just to throw my 2 cents in.... i personally advocate the use of
int *p1, *p2;
style syntax for the previously mentioned reasons.
ultimately this is a cosmetic issue though. do what makes most sense to you (and anyone else who might see your code)
-
July 25th, 2005, 01:10 PM
#9
Re: Best Way To Declare Pointers
Thank You for your responses, I am now, officially, much less confused 
Eric
-
July 25th, 2005, 02:13 PM
#10
Re: Best Way To Declare Pointers
At last, I have one thing in common with Cilu and gstercken that I declare it as int* myVariable too ..oh wait eveyone does it like that?
Good Answers, Good Points.
-
July 25th, 2005, 02:57 PM
#11
Re: Best Way To Declare Pointers
Let's see what the authors of the C language are doing.
For instance, B.W. Kerningham and D.M. Ritchie illustrate their discourse about pointers with
Code:
swap (px, py)
int *px, *py;
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
Well, they use "*px" like I do (but I don't use the odd and old syntax on two lines for the header of a function).
Personnally I prefer to be consistent and always glue the star next to the variable.
Otherwise you write "int* px" and "temp = *px;" (and not temp = * px; (not to mention the case of the multiplication when you write m = a * *px; instead of m = a * * p; )).
-
July 25th, 2005, 03:10 PM
#12
Re: Best Way To Declare Pointers
 Originally Posted by olivthill
Well, they use "*px" like I do (but I don't use the odd and old syntax on two lines for the header of a function).
Of course not - that way of declaring function parameters is deprecated since function prototypes have been introduced... 
 Originally Posted by olivthill
Otherwise you write "int* px" and "temp = *px;" (and not temp = * px).
But that are two completely different uses of the '*': In the first case, you declare a pointer variable, in the second case, you dereference it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|