char array wrong value

This is a discussion on char array wrong value within the C Programming forums, part of the General Programming Boards category; Hello, I am having trouble using a char array. I initialize the array along with another like so: Code: char ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    char array wrong value

    Hello,

    I am having trouble using a char array. I initialize the array along with another like so:

    Code:
    char hi[1024] = "", w[20] = "";
    The issue is that when I output the value of w, before assigning anything else to it, I get a value that is also found in another variable - like they are sharing memory - but not the exact same chunk.

    For example - say one char array contains these characters:
    abcdef

    w contains:
    def

    Anybody run into this before?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Sounds like you're corrupting the value in w somewhere between initializing it and printing it. Post a minimal example that demonstrates your problem, or some more code.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your arrays are local to functions, the contents outside what is being initialized explicitly is undefined. For example:

    Code:
    void foo(void)
    {
        char a[10] = "abcdefghi";
         ... // some more stuff 
    }
    
    void bar(void)
    {
        char b[10] = "z";
    
        for(i = 0; i < 10; i ++) 
            printf("b[%d] = %d (%c)\n", i, b[i], (b[i]?b[i]:'.'));
    }
    
    int main(void) {
       foo();
       bar();
    }
    This will print:
    b[0] = 122 (z)
    b[1] = 0 (.)
    b[2] = 99 (c)
    b[3] = 100 (d)
    b[4] = 101 (e)
    b[5] = 102 (f)
    b[6] = 103 (g)
    b[7] = 104 (h)
    b[8] = 105 (i)
    b[9] = 0 (.)

    If you wonder what this
    Code:
    (b[i]?b[i]:'.')
    means, it's a short way to say "print a dot" for the zero-byte indicating end of string.

    --
    Mats

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Mine prints b[0] = 122 (z), and that the remainder are 0.
    If there are fewer initializers in a list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    Which leads to:
    If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.
    That was the C89 draft. C99 explicitly states string literals:
    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    I get the remainder filled with 0 on both Linux/Windows with gcc, even if I use -std=c89. The C89 standard notes that initializing with:
    Code:
    char s[] = "abc"
    ...is identical to...
    Code:
    char s[] = { 'a', 'b', 'c', '\0' }
    ...and using the above rules, the remainder of the array should be zero filled.

    Now, whether you depend on that functionality is a different question altogether. Hopefully your compiler has at least C89.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const char* to char array.
    By kevinawad in forum C++ Programming
    Replies: 13
    Last Post: 08-05-2008, 02:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21