When you declare an array of chars and store a string in it, where isthe position of the null charac

Discussion in 'C Programming' started by Gary, May 5, 2008.

  1. Gary

    Gary Guest

    When you declare an array of chars and store a string in it, where is
    the position of the null character \0? And what happens to the unused
    memory locations?

    #include <stdio.h>
    int main(void)
    {
    char gstring2[25] = "dudes";
    gstring2[5] = 'a';
    printf("%s \n", gstring2);
    return 0;
    }

    The output of main function was

    dudesa

    How come this code works, and the statement
    gstring2[5] = 'a';
    doesn't overwrite the null character?
    Gary, May 5, 2008
    #1
    1. Advertising

  2. Re: When you declare an array of chars and store a string in it,where is the position of the null character \0? And what happens to the unusedmemory locations?

    Gary wrote:
    > When you declare an array of chars and store a string in it,
    > where is the position of the null character \0?


    Wherever you ask it to be, if you ask there to be one.

    > And what happens to the unused memory locations?


    Uninitialised elements of a struct or array object will
    be 'zero initialised'.

    > #include <stdio.h>
    > int main(void)
    > {
    > char gstring2[25] = "dudes";


    Same as...

    char gstring2[25] =
    { 'd', 'u', 'd', 'e', 's',
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    > gstring2[5] = 'a';
    > printf("%s \n", gstring2);
    > return 0;
    > }
    >
    > The output of main function was
    >
    > dudesa
    >
    > How come this code works, and the statement
    > gstring2[5] = 'a';
    > doesn't overwrite the null character?


    It does overwrite the null character, which was followed by
    another one.

    You need to be careful though of situations like...

    char foo[5] = "dudes";

    C, unlike C++, allows such an initialisation. There is no
    terminating null stored as there is no room for it.

    --
    Peter
    Peter Nilsson, May 5, 2008
    #2
    1. Advertising

  3. Gary

    Default User Guest

    Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

    Gary wrote:

    > When you declare an array of chars and store a string in it, where is
    > the position of the null character \0?


    After the last character of string data.

    > And what happens to the unused
    > memory locations?


    They are set to zero.

    > #include <stdio.h>
    > int main(void)
    > {
    > char gstring2[25] = "dudes";
    > gstring2[5] = 'a';
    > printf("%s \n", gstring2);
    > return 0;
    > }
    >
    > The output of main function was
    >
    > dudesa


    You overwrote the null terminator with a character.

    > How come this code works,


    Because there were extra characters that were set to zero due to the
    partial initialization.

    > and the statement
    > gstring2[5] = 'a';
    > doesn't overwrite the null character?


    It did, but a new terminator was in place.

    If you had this:


    char str[] = "dudes";

    Then you'd probably be in trouble. The next adjacent byte wouldn't even
    belong to your object so overwriting the null terminator would cause
    undefined behavior as soon as you tried a string operation.



    Brian
    Default User, May 5, 2008
    #3
  4. Gary

    Default User Guest

    Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

    arnuld wrote:

    > > On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:

    >
    > > You need to be careful though of situations like...
    > >
    > > char foo[5] = "dudes";
    > >
    > > C, unlike C++, allows such an initialisation. There is no
    > > terminating null stored as there is no room for it.

    >
    >
    > yes and you get garbage on the screen:


    > printf("%s\n", oye);


    No, you get undefined behavior.




    Brian
    Default User, May 6, 2008
    #4
  5. Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

    "rio" <> writes:
    > "arnuld" <> ha scritto nel messaggio
    > news:p...
    >>> On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:

    >>
    >>> You need to be careful though of situations like...
    >>>
    >>> char foo[5] = "dudes";
    >>>
    >>> C, unlike C++, allows such an initialisation. There is no
    >>> terminating null stored as there is no room for it.

    >>
    >> yes and you get garbage on the screen:
    >>
    >> #include <stdio.h>
    >>
    >> int main( void )
    >> {
    >> char oye[2] = "ok";

    >
    > is it not better oye[4] = "ok"; ?
    > "ok" is o+k+\0


    Not to illustrate the point, no. Both Peter Nilsson and Arnuld posted
    code that has an array that is not a string, and in both cases it was
    deliberate.

    If you want to fix the problem, it is hard to beat 'char oye[] = "ok";'
    since you then don't need a size. If you must have a size, anything
    other than 3 will be mildly confusing in this example.

    >> --
    >> http://lispmachine.wordpress.com/

    <snip>

    Best to trim your replies a bit more. In particular, remove sig blocks
    unless you are commenting on them.

    --
    Ben.
    Ben Bacarisse, May 6, 2008
    #5
  6. Gary

    arnuld Guest

    Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

    > On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:

    > You need to be careful though of situations like...
    >
    > char foo[5] = "dudes";
    >
    > C, unlike C++, allows such an initialisation. There is no
    > terminating null stored as there is no room for it.



    yes and you get garbage on the screen:



    #include <stdio.h>


    int main( void )
    {
    char oye[2] = "ok";

    printf("%s\n", oye);

    return 0;
    }

    ============= OUTPUT =============
    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
    /home/arnuld/programs/C $ ./a.out
    okHßÿ¿3.L
    /home/arnuld/programs/C $



    it *accidentally* terminated because at some random place in memory it
    found the NULL ?



    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above blog.
    just check the "About Myself" page :)
    arnuld, May 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,244
    Tim Rentsch
    Sep 23, 2005
  2. johnmmcparland
    Replies:
    3
    Views:
    492
    Gavin Deane
    Mar 29, 2007
  3. Hongyu
    Replies:
    9
    Views:
    883
    James Kanze
    Aug 8, 2008
  4. chris cannady

    ctypes return char array with null chars

    chris cannady, Apr 19, 2010, in forum: Python
    Replies:
    1
    Views:
    735
    Mark Tolonen
    Apr 19, 2010
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    213
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page