Thread: Clarification....

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Clarification....

    At various times I've seen and used both of the following...

    Code:
    int* test;
    
    int *test;
    I've also seen it used both ways in tutorials I've studied. Both appear to get the same result (a pointer to an int, in the example).

    Is there any meaningful difference?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read Stroustrup's take on the FAQ Is ``int* p;'' right or is ``int *p;'' right?
    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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Read Stroustrup's take on the FAQ Is ``int* p;'' right or is ``int *p;'' right?
    Yep, read that...

    Reason I'm asking is for some old code that has a bug I've never found.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If just moving the * causes the bug, then you have a buggy compiler.

    Code:
    int *    p, q;
    int     *p, q;
    Which of these helps you to figure out the type of q?
    Yes, this is very poor style, but it doesn't stop it from happening.
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    If just moving the * causes the bug, then you have a buggy compiler.
    That's not what I said... I said I wanted clarification if it *might* be the problem. The answer was no, which is good enough for me.


    Code:
    int *    p, q;
    int     *p, q;
    Which of these helps you to figure out the type of q?
    Yes, this is very poor style, but it doesn't stop it from happening.[/QUOTE]

    I don't stack declarations like that. As part of my commenting scheme I identify the function of each variable as I declare it.

    If I did however... I would use the second form since it is visually more indicative of function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clarification: behavior of type_info::before
    By Sebastiani in forum C++ Programming
    Replies: 11
    Last Post: 04-04-2008, 06:53 PM
  2. WM_DISPLAYCHANGE clarification
    By JasonD in forum Windows Programming
    Replies: 1
    Last Post: 11-10-2006, 02:27 AM
  3. Need a clarification here
    By bithub in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 12-27-2004, 01:06 AM
  4. exercise clarification truth table
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2003, 07:28 PM
  5. A little clarification?
    By Dr Nick in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 01:47 PM