Thread: Quick Pointer question

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Quick Pointer question

    This sounds quite noob. But when you declare a pointer, does it matter where the unary operator (*) is placed. Such as in,
    Code:
    int* p;
    or
    Code:
    int *p;
    Or is this just a preference thing?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Preference.
    As for myself I usually put the asterisk near the type for single variables and function heads,
    Code:
    int* p;
    void func (int* p);
    and the asterisk closer to the identifier when I declare many variables in one statement,
    Code:
    int *p, i;
    SDL_Surface *window, *font, *background;

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    It doesn't really matter, but just remember that it doesn't distribute across many variables. For example:
    Code:
    int* p, a, b, c;
    Only p is a pointer. You would need to do it like so:
    Code:
    int* p, *a, *b, *c;
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ah, I see what you mean there about it not distributing across the variables. I prefer the asterisk closer to the variable name, rather the data type. BTW you posted at the same time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  3. Declaring a Pointer (quick question)
    By viciousv322 in forum C Programming
    Replies: 4
    Last Post: 12-16-2005, 11:27 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM