C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-18-2009, 05:40 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 13
Question pointer initializatin - where to put the asterisk?

Hello there!
I am puzzled by a question lately, to wich I can't find an answer. What is the difference between the next pointer initialization methods:

Code:
int* pointer_name=NULL
and

Code:
int *pointer_name=NULL
I undestand, that the second one is the "standard" way of initializing a pointer, or at least that is how the tutorial on this website shows it, but what does the first one mean?

As I was searching the web for answers, I found another interesting example, where the pointer was initialized like this:

Code:
int * pointer_name=NULL
This was supposed to have some sort of special meaning, but I did not quite understand it, and I can't find the website now for further studying. I would be glad if someone could explain to me all these things about pointer initialization.

Big thanks in advance for everyone, reading this.
laczfinador is offline   Reply With Quote
Old 01-18-2009, 05:49 AM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
They mean exactly the same thing, it's a matter of style. I prefer the third one personally.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 01-18-2009, 06:55 AM   #3
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Just keep in mind that doing

Code:
int* p1, p2;
Does not declare two pointers, as if int* was a new type declaration. You could use a typedef to make an alias if you wanted to, but that is another story. If you want to declare two pointers, you have to do something like this:

Code:
int* p1, *p2;
or

Code:
int* p1, * p2;
Whatever you end up doing, try to be consistent in your use of it.
__________________
Got Ed?

sys-sizes
kermit is offline   Reply With Quote
Old 01-18-2009, 07:19 AM   #4
Registered User
 
Join Date: Dec 2008
Posts: 13
Question Well, here's an example...

Hm... I am trying to understand how does the next function works. It is from a SDL game programming tutorial.

Code:
SDL_Surface *load_image(char filename[])
	{
	SDL_Surface* loadedImage=NULL;
	SDL_Surface* optimizedImage=NULL;

	loadedImage=SDL_LoadBMP(filename);
	if(loadedImage!=NULL)
		{
		optimizedImage=SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
		}
	return optimizedImage;
	}
As you can see, both methods are used in this function definition - but why? Is this really just a matter of style or does it make some crucial difference if I put the asterisk to another position? As far as I tested, the program runs and compiles fine, when I play around with the asterisks, so - can it be, that the author of the tutorial wasn't consistent enough?

Thanks for the answers so far!
laczfinador is offline   Reply With Quote
Old 01-18-2009, 07:58 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
http://www.research.att.com/~bs/bs_faq2.html#whitespace
Bjarne "explains" the difference between the two styles.
I prefer the first one, and find the 3rd one acceptable, as well.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-18-2009, 08:54 AM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
I like the 2nd one because it makes the most sense, and you can consider it the same as dereferencing
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 01-18-2009, 08:55 AM   #7
Registered User
 
Join Date: Dec 2008
Posts: 13
Thumbs up Thanks a lot!

Now, that is the kind of answer I was looking for! Thanks a lot everyone, for helping me out!
laczfinador is offline   Reply With Quote
Old 01-18-2009, 08:59 AM   #8
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by MK27 View Post
I like the 2nd one because it makes the most sense, and you can consider it the same as dereferencing
The second one makes absolutely no sense to me, whatsoever...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-18-2009, 11:20 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by laczfinador
can it be, that the author of the tutorial wasn't consistent enough?
Yes, there is a minor inconsisteny in style there.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-18-2009, 11:36 AM   #10
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Quote:
Originally Posted by Bjarne Stroustrup
placing the * closer to the name does not make this kind of error significantly less likely.
I disagree, but will grant that obviously my opinion as a programmer matters very little compared to someone as experienced as Stroustrup. That said, when I see

Code:
double *p, q;
it is immediately obvious to me that p is the pointer, and q is just a double.* For the original poster, seeing as you are posting this on the C board, have a look at this and this from the comp.lang.c FAQ. Steve Summit acknowledges the difference of opinion of the C FAQ and Stroustrups C++ FAQ.

I think Stroustrup makes a good point about declaring each pointer one per line. Again, whichever style one chooses, it is good to be consistent.

* - I would add that I am undoubtedly biased by the fact that C is the first language I learned and though I dabble in other languages (most recently I have taken up the task of becoming competent in C++ programming) it is still my 'primary' language. I have also gotten used to K&R style. Had I learned C++ first, I guess I would probably be more inclined to the 'Stroustrup style' of declaring a pointer with the asterisk next to the type rather than the name...
__________________
Got Ed?

sys-sizes

Last edited by kermit; 01-18-2009 at 12:31 PM.
kermit is offline   Reply With Quote
Reply

Tags
asterisk, initialization, pointer, position

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ban pointers or references on classes? Elysia C++ Programming 89 10-30-2007 03:20 AM
sorting with pointer of pointers to array dunpealslyr C++ Programming 6 10-01-2007 11:26 PM
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM
God datainjector A Brief History of Cprogramming.com 746 12-22-2002 12:01 PM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM


All times are GMT -6. The time now is 07:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22