Thread: One little question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26

    One little question

    What is the difference(if any??) on those 3:

    int* p;
    int * p;
    int *p;


    is there any difference?


    I tried this

    Code:
        
        int* p1;
        int n1 = 5;
        p1 = &n1;
        cout << *p1 << endl;
        
        int *p2;
        int n2 = 5;
        p2 = &n2;
        cout << *p2 << endl;
        
        
        int * p3;
        int n3 = 5;
        p3 = &n3;
        cout << *p3 << endl;
    and they all output 5, so, I'm curious.

    Dag

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there any difference?
    No - they're all the same to the compiler.

    > int* p;
    Except this suggests a precedence which isn't true, which is more likely to affect the reader of the code.

    Example
    int *p,q;
    p is a pointer to an int, q is just an int.

    So saying
    int* p,q;
    makes it look more likely that p and q are both pointers (which would be wrong).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    thank you

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int* p;
    It's amazing how many programmers use this one. Like Salem, I also prefer:
    >int *p;

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Or you could use the formatting scheme suggested by galmca. By completely omitting all whitespace, you reduce the size of a source code file, making it faster to transmit. Consider the truly elite form: int*p;

    Of course, I merely suggest this in jest. Using the form Salem suggested has kept me from decieving myself several times. It's a good habit to acquire.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM