Thread: Setting a char * to null

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    16

    Setting a char * to null

    Hello,

    I have a pointer which holds a variable for example:

    char * username;
    strcpy(username, "ted");

    How do you set the pointer so its empty and does not hold the name ted?


    I've tried setting the pointer to null but keep getting a segmentation error.

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    for starters that code won't work anyway because the pointer doesn't point to memory you own. you have to allocate memory to the pointer first, or point it to an array. once you have that you can null out a string by setting the first character to 0.
    Code:
    username[0] = '\0';
    that works because strings are just arrays of char with a 0 at the end. if the 0 is at the beginning, it's an empty string.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    you need to allocate memory for the pointer before you do the strcpy function...

    username = (char *) malloc=(sizeof(char) * strlen("ted"));
    strcpy(username, "ted");
    username = NULL;

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Bladactania
    username = NULL;
    that doesn't work. well it works, but it doesn't work well. if you allocate memory to username then set it to null, you lose the only reference you had to the memory and can't free it. that's called a memory leak. if you point username to an existing array you can set it to null but that doesn't change the actual string. it just means the pointer no longer points to the string.

  5. #5
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Do you actually want to remove the data or just free the memory? These aren't the same things if I recall correctly.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by Meldreth View Post
    if the 0 is at the beginning, it's an empty string.
    Just in order to maintain my image of being a nitpicker: (char *)NULL is the empty string, "\0" is a string of size 0.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Snafuist
    Just in order to maintain my image of being a nitpicker: (char *)NULL is the empty string, "\0" is a string of size 0.
    I think your nitpicking is incorrect: (char*)NULL is a null pointer, but "\0" is an empty string. "" is also an empty string, as is "\0abc".
    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
    Feb 2009
    Posts
    278
    Quote Originally Posted by Meldreth View Post
    that doesn't work. well it works, but it doesn't work well. if you allocate memory to username then set it to null, you lose the only reference you had to the memory and can't free it. that's called a memory leak. if you point username to an existing array you can set it to null but that doesn't change the actual string. it just means the pointer no longer points to the string.

    Well I had assumed he would free the memory later... Guess that was a bad assumption since he didn't know the need to allocate it in the first place.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    that's wrong though. (char*)NULL isn't a string at all, it's a null pointer. this isn't a string either even though it's an array of char.
    Code:
    char im_not_a_string[] = {'a', 'b', 'c'};
    the definition of a string is a contiguous sequence of characters terminated by '\0'. no '\0', no string.
    Quote Originally Posted by Bladactania
    Well I had assumed he would free the memory later...
    how can he free it if he doesn't have a pointer to it? setting username to NULL means you don't have a pointer to free anymore.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You can't have it both ways... if null means I don't have a pointer to free anymore then freeing it is the same as setting it to NULL.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    You can't have it both ways... if null means I don't have a pointer to free anymore then freeing it is the same as setting it to NULL.
    No, they are not the same. Setting the pointer to be a null pointer does not free memory, and using free() does not set the pointer to be a null pointer.
    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

  12. #12
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Hi laserlight,

    I think your nitpicking is incorrect: (char*)NULL is a null pointer, but "\0" is an empty string. "" is also an empty string, as is "\0abc".
    We have a different opinion about what a string is. To me, there's a difference between a string and a mere character array. A string is a memory area, terminated by the first occurrence of '\0'. Hence, I don't regard "\0abc" as being a string of size 4. To me, this is either a string of size 0 or a character array of size 4.

    Thus, "" is a string of unknown size, probably far from being empty.

    My main interest is complexity theory, where some languages (language as simply a set of words) contain the empty word, often called epsilon, which is different from a word of size 0 (in much the same way as NULL is different from "\0"). I might add that programming is just a hobby for me, so I'm happy with whatever meaning is in use for "empty string" around here. Just nitpicking...

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    so you can't free a pointer that has been set to NULL or 0?

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Snafuist
    A string is a memory area, terminated by the first occurrence of '\0'.
    that contradicts what you said before. NULL can't be an empty string if a string is a memory area terminated by the first '\0'. a null pointer is neither a memory area nor terminated by '\0'. it's a pointer to nowhere, with a predictable value so you know you're pointed to nowhere.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Bladactania View Post
    so you can't free a pointer that has been set to NULL or 0?
    you can always say free(NULL) or free(0). but that doesn't change anything.
    Code:
    p = malloc(n); // allocate memory
    p = NULL; // forget where you allocated the memory
    free(p); // does nothing
    // the memory is still allocated!
    // you can't free it because you pointed p somewhere else

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. last stage is stuck in school work
    By vinuk23 in forum C Programming
    Replies: 8
    Last Post: 03-05-2009, 04:03 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM