CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2004
    Posts
    33

    Pointer * Question

    I am still familiar with the use of pointers but I have been away from this for a couple years so I'm looking through many tutorials.

    One question has come to mind lately and I have hesitated asking for fear of embarassment but... the only stupid question is the one you know the answer to.

    What, if any, difference is there in declaring pointers in the two following manners.

    // I know this is already defined... just example
    struct Rect
    {
    int x;
    int y;
    int width;
    int height;
    };

    Rect* Head;
    Rect *Tail;

    Does this make any difference in functionality or memory usage? I know it's almost trivial, I've been waiting for the books and tutorials I am reading to explain, it's illustrated in both formats.

    Thanks for any help I receive!
    The Beginner

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Pointer * Question

    Originally posted by Ralph_Brickley
    One question has come to mind lately and I have hesitated asking for fear of embarassment but... the only stupid question is the one you know the answer to.
    There isn't such a thing as a stupid question...there are only stupid answers...

    Originally posted by Ralph_Brickley
    What, if any, difference is there in declaring pointers in the two following manners.

    Rect* Head;
    Rect *Tail;
    There is no difference...except that the first pointer is named 'Head' and the second one 'Tail'...

  3. #3
    Join Date
    Feb 2004
    Posts
    33
    Thank you Andreas! I figured that to be the case but I wanted to be sure. I'm downloading and purchasing large volumes of books and tutorials getting ready to embark on game programming, mostly 3D!
    The Beginner

  4. #4
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    Just as a side comment, I would personally prefer

    Code:
    Rect *Head;
    over
    Code:
    Rect* Head;
    simply to avoid confusion.

    Keep in mind that
    Code:
    Rect *Head, *Tail;  // declares two pointers
    is NOT the same as
    Code:
    Rect* Head, Tail; // only declares Head as a pointer

  5. #5
    Join Date
    Feb 2004
    Posts
    81
    I agree with bluesource but again it is a matter of choice and style.
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  6. #6
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    omg bluesource right on! so often i'm asked to find the error in a program and it's cuz someone did stuff like that assuming all the variables would be declared as pointers. how they got that idea is beyond me but just thought i'd chime in and say i agree 110%

  7. #7
    Join Date
    Feb 2004
    Posts
    81
    What else we can do if language agrees with what they write.
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  8. #8
    Join Date
    Aug 2002
    Posts
    78

    Re: Pointer * Question

    > I have hesitated asking for fear of embarassment but... the
    > only stupid question is the one you know the answer to.

    And even then, sometimes it must be asked.

    E.g. "Sandra Bullock, Nicole Kidman, and Charlize Theron, will one of you go out on a date with me?"

    Better to ask and get shot down than to live a lifetime wondering what might have been. Specifically, what the sound of a definite "no" would have been on your eardrums.

  9. #9
    Join Date
    Feb 2004
    Posts
    81
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  10. #10
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Re: Pointer * Question

    Originally posted by Andreas Masur
    There isn't such a thing as a stupid question...there are only stupid answers...
    Let me just say that I was a teacher for several years and
    THERE IS SUCH A THING AS A STUPID QUESTION!!


    p.s. I am not saying that the question which started this thread
    was stupid.
    Last edited by souldog; February 23rd, 2004 at 03:20 AM.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    It helps to understand whitespace, which is essentially space characters, tabs and end-of-lines. In the C/C++ language standards, whitespace is ignored (not relevant) except for delimiting tokens. Whitespace is irrelevant to the syntax otherwise. So as far as the compiler is concerned, the sample code looks like:
    Code:
    Rect*Head;
    Rect*Tail;
    Note that whitespace is relevant in the following:
    Code:
    Rect Head;
    Rect Tail;
    However the compiler does not care how much whitespace there is; just that there is whitespace between "Rect" and the variable being defined.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Feb 2004
    Location
    Montréal, Québec, Canada
    Posts
    49

    Re: Re: Re: Pointer * Question

    Originally posted by souldog
    Let me just say that I was a teacher for several years and
    THERE IS SUCH A THING AS A STUPID QUESTION!!
    I actually love Despair.com's reply on that : "There are no stupid questions, but there are a lot of inquisitive idiots"

    I'm not saying this applies to any of you though
    After three days without programming, life becomes meaningless.
    - The Tao of Programming, book 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured