Thread: pointer initializatin - where to put the asterisk?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    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.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    They mean exactly the same thing, it's a matter of style. I prefer the third one personally.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    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!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I like the 2nd one because it makes the most sense, and you can consider it the same as dereferencing
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    Thumbs up Thanks a lot!

    Now, that is the kind of answer I was looking for! Thanks a lot everyone, for helping me out!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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...
    Last edited by kermit; 01-18-2009 at 12:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread