Thread: Origin of pointer syntax

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Origin of pointer syntax

    I've always been curious why you declare a pointer like this:
    Code:
    aType * ptr;
    you ask for a reference like this:
    Code:
    &something; // give me a reference to something
    and you dereference with the same thing you declare a pointer with:
    Code:
    *something; // give me the value of something
    Why is that? What is the commonality between declaring a pointer and dereferencing it?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    Why is that? What is the commonality between declaring a pointer and dereferencing it?
    Compare these two declarations:

    Code:
    int x;
    int *y;
    The first one says, "x is an integer." The second one, although technically declaring a pointer, can be thought of as saying, "*y is an integer." Therefore, y must be "something" which, when you stick a star on the front of it, is an integer. In other words, a pointer to an integer.

    So the declaration syntax is borrowing from the dereferencing syntax, not the other way around.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you have
    Code:
    int * ptr;
    then ptr is a pointer-to-int and *ptr is an int, all at the same time.

    IOW, they needed something in the declaration to say "this is a pointer", and using the same symbol * as dereferencing allowed the pun above, without using up another keyword or symbol.

    (I seem to recall somebody smart and important saying much the same thing but with more authority -- maybe Stroustrup in his FAQ?)

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by brewbuck View Post
    The second one, although technically declaring a pointer, can be thought of as saying, "*y is an integer."

    So the declaration syntax is borrowing from the dereferencing syntax, not the other way around.
    Yep, that clears it up for me.

    Thanx.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM