Thread: How come char pointer doesn't need an address sign?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    24

    How come char pointer doesn't need an address sign?

    This is how normal pointer works...

    Code:
    int x = 5;
    int *pX = &x;

    But how come char doesn't work that way?

    Code:
    char y[] = "Hello!";
    char *pY = y;
    Why does this work even if I don't put & sign?

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    y in your example is not a char, is a (nul-terminated) array of char. In C, arrays are in many ways equivalent to a pointer to their 1st element. In your example, pY points to the address of 'H' (the 1st letter in your char array).
    Last edited by migf1; 08-10-2015 at 12:27 AM.
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    24
    I have another question.

    Code:
    int *x = 5;
    If pointer takes an address, how come the code above is executable? I mean in C++ I can't even compile this code...

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    You will get a warning have to type cast to integer pointer. I think you will also get a segmentation fault if you try to dereference x.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by yj1214 View Post
    I have another question.

    Code:
    int *x = 5;
    If pointer takes an address, how come the code above is executable? I mean in C++ I can't even compile this code...
    What you meant there was that your able to compile the above code, but not able to execute it. Because you wont be able to execute that code. Your trying to dereference a block of memory which dosnt belong to your process. That is, 5 is an arbitrary address as it is, you dont know if does even exists on your machine. The pointer can take any value. The problem only hits when you try to dereference it. In your example if your try to de-reference the x; your bound to get segfault. Does that make sense?
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    24
    Sorry guys i'm holding this topic too long but I just wanted to make sure that i'm understanding this correctly.

    Code:
    char *sentence = "Hello World!";
    [/code]

    sentence is a pointer variable that is holding an address of the string(arrays of chars) "Hello World!". Am I correct?

    So "Hello World!" is like a variable that has no name, like this,
    Code:
    char[] *blank* = "Hello World!";

    And for some reason, we don't need to write
    Code:
    char *sentence = &"Hello World!"
    . Why is this?


    Code:
    char sentence[] = "Hello World!";
    char *p_sentence = &sentence;
    Why can't I put & next to sentence?


    I'm sorry guys i'm just terrible at understanding pointer...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yj1214
    sentence is a pointer variable that is holding an address of the string(arrays of chars) "Hello World!". Am I correct?
    Close. The string literal "Hello World!" is an array of 13 chars. In most contexts, including here, an array is converted to a pointer to its first element. So, sentence is a pointer to char rather than a pointer to an array of 13 chars because it points to the first element of "Hello World!". Because string literals cannot be modified, we would normally use const char* instead:
    Code:
    const char *sentence = "Hello World!";
    Quote Originally Posted by yj1214
    Why can't I put & next to sentence?
    This is one of those cases where the array is not converted to a pointer to its first element. &sentence results in a pointer to an array of 13 chars, i.e., a char (*)[13], but what you want is a char*.
    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

  8. #8
    Registered User
    Join Date
    Mar 2015
    Posts
    24
    Alright, correct me if i'm wrong.


    Code:
    char *sentence = "Hello";
    sentence holds an address of a first element of the array which would be 'H' in this case.


    Code:
    char sentence[] = "Hello";
    char *p_sentence = sentence;
    p_sentence would store the address of value 'H' and it knows where the array ends because there is a nul-terminator at the end of the string literal.


    Code:
    char *x = &"Hello";
    
    char y[] = "Hello";
    char *z = &y;
    Is there specific reason why I don't have to put &? Why can't compiler know that i'm talking about the first elemnt of "Hello" which appears to be 'H' (the address of 'H')?

    It seems more weird that I don't have to put & because all other data type needs it except char,

    Is it just whoever made C decided to do this way?


    Code:
    char *x = "Hello";
    Is same as

    Code:
    char x[] = "Hello";
    char *y = x;
    The only difference is that second one can be modified...correct?



    Last of all,

    Code:
    const char *x = "Hello";
    const is not neccecery but it's good idea to put it because we can't modify the value that x is pointing to.

    And we can't modify it because x is pointing to the first element of an array and it doesn't know other arrays like 'e' or 'l'...
    Last edited by yj1214; 08-11-2015 at 02:00 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yj1214
    Is there specific reason why I don't have to put &?
    The specific reason is that if you have an array x, &x results in a pointer to the array itself, not to a pointer to its first element. So, not only do you not have to put the &, but doing so would be wrong.

    Quote Originally Posted by yj1214
    Why can't compiler know that i'm talking about the first elemnt of "Hello" which appears to be 'H' (the address of 'H')?
    Because you would have explicitly told the compiler that you are talking about the entire array, not the first element thereof. It so happens that they have the same address, but the types are different.

    Quote Originally Posted by yj1214
    It seems more weird that I don't have to put & because all other data type needs it except char
    That's not true. For example, consider:
    Code:
    int y[] = {123, 456, 789};
    int *z = &y;
    The second line in the above code would likely result in a warning: &y is of type int(*)[3], but z is of type int*. The correct code would be:
    Code:
    int *z = y;
    or equivalently:
    Code:
    int *z = &y[0];
    Quote Originally Posted by yj1214
    Code:
    char *x = "Hello";
    Is same as

    Code:
    char x[] = "Hello";
    char *y = x;
    The only difference is that second one can be modified...correct?
    No. In the former, x is a pointer to the first element of a string literal. In the latter, x is an array of char.

    Quote Originally Posted by yj1214
    Code:
    const char *x = "Hello";
    const is not neccecery but it's good idea to put it because we can't modify the value that x is pointing to.
    Yes.

    Quote Originally Posted by yj1214
    And we can't modify it because x is pointing to the first element of an array and it doesn't know other arrays like 'e' or 'l'...
    No, you cannot modify it because it is part of a string literal, and modifying a string literal results in undefined behaviour. The implementation might place storage for the string literal in a location that cannot be modified, for example.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing the Address of a char???
    By DiscoStu9 in forum C Programming
    Replies: 13
    Last Post: 10-08-2008, 05:28 PM
  2. pointer and smart pointer address
    By l2u in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2006, 05:00 PM
  3. On the sign of char
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 10:00 AM
  4. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:05 AM
  5. sign extension char
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 08:28 AM