Thread: Setting a char * to null

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by Snafuist
    To me, there's a difference between a string and a mere character array.
    Definitely.

    Quote Originally Posted by Snafuist
    Hence, I don't regard "\0abc" as being a string of size 4.
    It is not a string of size 4. Rather, it is a string of length 0 (or not a string at all, if you consider string literals with embedded null characters as merely char arrays).

    Quote Originally Posted by Snafuist
    To me, this is either a string of size 0 or a character array of size 4.
    It is not a char array of size 4. It is a char array of size 5, since the terminating null character must be counted.

    Quote Originally Posted by Snafuist
    Thus, "" is a string of unknown size, probably far from being empty.
    "" is a string literal consisting of a null character. As such, it is of length 0, and is an empty string.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Bladactania View Post
    so you can't free a pointer that has been set to NULL or 0?
    It is legal to do so, but it is a no-op (it does not actually free any memory).

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Snafuist View Post
    Thus, "" is a string of unknown size, probably far from being empty.
    Technically in C, "" is null terminated and has a length of zero.

    I read somewhere the other day that Chomsky's "mathematical model for grammars" (1956) was practically applied more in computing than it ever was in linguistics. But I don't want to start anything nasty...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    laserlight, I love you. I could talk about stuff like that all day long.

    Quote Originally Posted by laserlight View Post
    It is not a string of size 4. Rather, it is a string of length 0 (or not a string at all, if you consider string literals with embedded null characters as merely char arrays).
    No, I'm happy to regard the first part of it as a string of length 0. The whole thing altogether is still either a string of length 0 or a character array of size 4 or 5 (see below) or two strings concatenated together with their null-bytes or (just to make it more complicated) a string of size 0 concatenated with a character array of size 3.


    It is not a char array of size 4. It is a char array of size 5, since the terminating null character must be counted.
    Oh, this is probably my fault. I was thinking that you wrote "\0abc" as shorthand for
    Code:
     {'\0', 'a', 'b', 'c'}
    as in "\0abc" is part of "foo\0abcdefg". If the "" signs denote C strings, you are of course right. Then, according to my understanding, the size is 4, and according to your intention, the size is 5. I'm completely with you so far.


    BTW, I'm in the process of writing you (among others) a private message. Shall I use PM or email?

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

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