Thread: Why is char *p =s; valid?

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    Why is char *p =s; valid?

    Hi,

    I learned a bit of C a few years ago, but since it was not for one of my university courses(a bit of C++ and quite a bit more Java, Python and C#) my study of the language did not go all that far, so I apologize if my questions sounds like total noob lunacy, I merely wish to rectify the huge mystake of not really learning C.

    So I was browsing some code, and basically address assignment here is done at pointer declaration, however since the '*' character is also used for dereferencing as well is it not more proper to first declare the pointer, and only then to assign it something to point to, like this:

    char *p;
    p=s;

    instead of char *p=s:

    Shouldn't that be more clear and safe, can somebody please explain the rational behind this type of assignment to me?

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    At it's declaration though, it wouldn't really make sense for the pointer to be dereferenced in an assignment. It isn't pointing to anything yet, so you couldn't modify the value of anything it points to at that time. It does look a little like dereference though, that's why I always write a declaration of a pointer like this:

    Code:
        char* p; // It looks clearer that the identifier p is pointer to char
    Everyone does it a little different, the worst looking to me is this :
    Code:
        char * p; // I see this in books sometimes, and always immediately think multiplication is going on lol.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    In the following overly simplified example, I have defined a pointer to char called s, and initialized it to the address of the first character of a constant string;

    Code:
    #include <stdio.h>
    int main(void)
    {
       char *s = "Hello World.";      /* s is a pointer to a constant string. */
    
       char *p = s;                  /* p is a pointer to a char and inistialized
                                        with the value of s  */
    
       printf("%c\n", *p);            /* Print the first character of the string using p  */
    
       return 0;
    }
    I have then defined another pointer to a char called p, and initialized the pointer with the address that is stored in the pointer, s. The '*' in both cases define both p & s as pointers.

    The printf() statement uses the '*' to dereference the pointer to print the first character of constant string.

    The '*' is used to both declare/define, AND dereference in different contexts.

    You need to study arrays and pointers to gain a better understanding.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    Quote Originally Posted by rstanley View Post
    In the following overly simplified example, I have defined a pointer to char called s, and initialized it to the address of the first character of a constant string;
    Code:
    #include  int main(void) {    char *s = "Hello World.";      /* s is a pointer to a constant string. */     char *p = s;                  /* p is a pointer to a char and inistialized                                     with the value of s  */     printf("%c\n", *p);            /* Print the first character of the string using p  */     return 0; }
    I have then defined another pointer to a char called p, and initialized the pointer with the address that is stored in the pointer, s. The '*' in both cases define both p & s as pointers. The printf() statement uses the '*' to dereference the pointer to print the first character of constant string. The '*' is used to both declare/define, AND dereference in different contexts. You need to study arrays and pointers to gain a better understanding.
    Yeah, I got that, I am just saying that maybe both declaring and assigning might not be the best idea where pointer are concerned, I was merely wondering why such misleading syntax was allowed in the first place. Thanks for your replies.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KnRfan
    I am just saying that maybe both declaring and assigning might not be the best idea where pointer are concerned, I was merely wondering why such misleading syntax was allowed in the first place.
    It is not misleading once you recognise that it is an initialisation done at the point of declaration rather than an assignment done after the declaration. Furthermore, while many times this initialisation might be for convenience, if you have a pointer that is const, you will need to do such initialisation since you cannot validly assign to the pointer later.

    Quote Originally Posted by Alpo
    Code:
    char* p; // It looks clearer that the identifier p is pointer to char
    I recommend reading Stroustrup's answer to 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

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by KnRfan View Post
    Yeah, I got that, I am just saying that maybe both declaring and assigning might not be the best idea where pointer are concerned, I was merely wondering why such misleading syntax was allowed in the first place. Thanks for your replies.
    I always initialize my variables to some appropriate value, and all my pointers to a valid address or NULL. For example:
    Code:
    int count = 0;
    char *ptr = NULL;
    //etc...
    Why do either one as two statements, when one will define and assign in both examples? The compiler is probably creating the same code in both cases. I find the definition, and initialization as one statement to be more clear. If two statements would be more clear to you then by all means do so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can not get valid value from Char* buffer
    By jeffwang in forum C Programming
    Replies: 6
    Last Post: 01-05-2012, 05:00 PM
  2. Checking if char *path; is a valid directory.
    By b1nd3r in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 02:55 PM
  3. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  4. (char)09 not valid?
    By mikeyp in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2009, 09:09 PM
  5. Is this valid?
    By KBriggs in forum C++ Programming
    Replies: 3
    Last Post: 07-08-2009, 10:42 AM