Thread: Pointer Question

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Pointer Question

    In my quest to understand pointers, I have started to wonder something. In the following lines of code:

    Code:
    char * MyName = "Paul";
    cout << *MyName << endl;
    The program outputs 'P' because P is the value being pointed to by MyName. Is that correct?

    Code:
    char * MyName = "Paul";
    cout << MyName << endl;
    This should print the address that MyName points to shouldn't it? But instead it prints the string "Paul". Is this because cout is smart enough that when it gets a char *, it will start from that address and keep printing values until it finds a \0?

    Also, when I write
    char * MyName = "Paul"
    Does MyName actually hold the address of the static string "Paul" in memory? Thanks for any help!

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Pointer Question

    Originally posted by stovellp
    In my quest to understand pointers, I have started to wonder something. In the following lines of code:

    Code:
    char * MyName = "Paul";
    cout << *MyName << endl;
    The program outputs 'P' because P is the value being pointed to by MyName. Is that correct?
    Yes.


    Code:
    char * MyName = "Paul";
    cout << MyName << endl;
    This should print the address that MyName points to shouldn't it? But instead it prints the string "Paul". Is this because cout is smart enough that when it gets a char *, it will start from that address and keep printing values until it finds a \0?
    Yes

    Also, when I write
    char * MyName = "Paul"
    Does MyName actually hold the address of the static string "Paul" in memory? Thanks for any help!
    Yes. Although, more properly, you should use

    const char * MyName = "Paul";

    The string literal "Paul" is interpreted as the address where a constant string with the data "Paul" lives. As the string is const, you should use a pointer to const (const char *) not a pointer to non-const (char *).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ahhh ok, thanks Cat my understanding of pointers just improved dramatically

    Edit: So really, cout is lying to me when I tell it to print MyAddr, because it should print the address. It just ASSUMES I want to print the entire string. How can I tell it to print the address of "Paul"? I cant say &MyName because thats the address of the pointer itself, not the address that MyName points to. Is that correct?

    Also, am I right with this explanation:

    Code:
    void CallAFunction(int * Age)
    {
      *Age = 45;
      return;
    }
    
    int main()
    {
      int MyAge = 20;
      CallAFunction(&MyAge);
      cout << MyAge;
    }
    It will print 45. When it makes the call to CallAFunction, which takes a pointer as a paramater (which is really another variable the same as all the rest, its value is just an address), it sends the address of MyAge.

    Really, its kind of the same as passing an integer, except the integer is the address of MyAge, and the function takes that address and changes the value pointed to at that address. Wow... I think I understand this
    Last edited by nickname_changed; 10-10-2003 at 03:05 AM.

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Also, the line:

    const char * MyName = "Paul";

    Is actually the same as

    const char * MyName = &"Paul";

    And really, the second line makes more sense since a pointer holds an address of a string, not a string. But is it the compiler thats smart enough to figure out you mean to write &"Paul" rather than "Paul"?

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    So really, cout is lying to me when I tell it to print MyAddr, because it should print the address. It just ASSUMES I want to print the entire string. How can I tell it to print the address of "Paul"? I cant say &MyName because thats the address of the pointer itself, not the address that MyName points to. Is that correct?
    An adress is an integer, so, use a cast to an integer and you're done. Or so I think...

    And really, the second line makes more sense since a pointer holds an address of a string, not a string. But is it the compiler thats smart enough to figure out you mean to write &"Paul" rather than "Paul"?
    ... to me, you're wrong, a litteral string is an array of characters and in C, array identifiers expand to the adress where lies the forst element of the array, so "Paul" should be the adress of the first element. It's only my opinion though.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    How can I tell it to print the address of "Paul"?
    cout << reinterpret_cast<void*>(MyName) << endl;

    ... to me, you're wrong, a litteral string is an array of characters and in C, array identifiers expand to the adress where lies the forst element of the array, so "Paul" should be the adress of the first element.
    This is semi-correct. Array names do decay into pointers, but it is also important to realize that they do not decay into pointers of the element type - they decay into pointers of array type. So, for an array of characters, the array name would decay into a pointer to an array of characters - not a pointer to a character.

    Edit:
    I stand corrected. I shouldn't post in a hurry - The array itself simply decays into a pointer to the first element as stated by lyx.
    &array returns a pointer to an array of element type.
    Last edited by thefroggy; 10-10-2003 at 07:50 PM.

  7. #7
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, pointers they are not, they are constant pointers to be exact. Besides, I was right, BS § 5.3 specifies that arrays are implictly converted into pointer to the type of their elements.
    An array *is* an array, however, it has an implicit conversion to a pointer to its element type.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by lyx
    Well, pointers they are not, they are constant pointers to be exact. Besides, I was right, BS § 5.3 specifies that arrays are implictly converted into pointer to the type of their elements.
    An array *is* an array, however, it has an implicit conversion to a pointer to its element type.
    Absolutely. So for a single dimension array of ints there's a conversion to a pointer to int (the first element).

    For a multidimension int array, we have - not an array of ints - but an array of arrays (of ints). The conversion is once again to a pointer to the first element, which in this case is a pointer to an array (of ints) rather than a pointer to int.

  9. #9
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks heaps guys I think I'm finally starting to understand pointers

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by lyx
    Well, pointers they are not, they are constant pointers to be exact.
    No, they are pointers to const (const char *) not constant pointers (char * const). Well, the pointers are const, as well, just like any literal is always const, so I suppose you could say it is a const pointer to const (const char * const).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Excuse me but I was talking about arrays in general, they aren't all pointers to constant data but they are all constant pointers. ^^

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Ah, I misunderstood. You are of course correct, an array will automatically convert to a constant pointer, not a non-const pointer, and thus is not legal as an lvalue.

    In the specific case of string literals, though, they are const pointer to const.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM