char *p uninitialized

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    char *p uninitialized

    I had an error in my code and I distilled it down to this: Could somebody tell me why the following program claims that p is being used uninitialized?
    [code=cpp]
    #include <iostream>
    int main()
    {
    char *p; // error: p is being used uninitialized
    // char p[10]; // no problem if we do this instead
    strcpy(p,"Hello ");
    return 0;
    }
    [/code]
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Because it is. To initialize a variable, you set it equal to something. This is especially important with pointers, as the pointer will still have whatever garbage value it had the last time that memory was used. This value may or may not (probably not) represent a valid memory address, and if it is valid, the program may not be allowed to use it by the OS. On the other hand, when you declare an array, because you must give it a size, the compiler allocates memory for the array, initializing the pointer to a safe address.

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      Yes, duh, that makes sense. Thanks. :-)

      Comment

      • questionit
        Contributor
        • Feb 2007
        • 553

        #4
        Originally posted by arnaudk
        I had an error in my code and I distilled it down to this: Could somebody tell me why the following program claims that p is being used uninitialized?
        [code=cpp]
        #include <iostream>
        int main()
        {
        char *p; // error: p is being used uninitialized
        // char p[10]; // no problem if we do this instead
        strcpy(p,"Hello ");
        return 0;
        }
        [/code]
        Hi

        Whenever you declare a pointer, it must be initialized before you use it.

        For example:
        [code=c]
        char myArray[20];
        char *myPointer = myArray;
        //now you can use myPointer and assign it a value in your code.
        [/code]

        You might want to initialize your pointer at time of declaration:
        For example:
        [code=c]
        char *myString = "abcd";
        [/code]
        Someone might say it should be like this:
        [code=c]
        const char *myString = "abcd";
        [/code]

        These 2 are the same things but perhaps it is better to declare a pointer as constant if you want to initialize it with a string at time of declaration. (Note: This string would be a constant string)

        An easy way
        You can use C++'s
        [code=c]
        using std::string
        [/code]
        so you won't have to worry about the pointer initializing, etc.

        Hope it helps
        Qi

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Yes, thanks. I think my confusion stemmed from my assumption that I could use strcpy to perform the initialization. Indeed,

          char *p;
          strcpy(p,"Hello ");

          is not equivalent (although syntacticaly it appears to be), to

          char *p = "Hello";

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            I don't think they look syntactically equivalent, although you might think they semantically mean the same thing. To truly understand the difference, you should know how to distinguish between arrays and pointers, and understand that a pointer has to...actually point somewhere.

            Comment

            Working...