char array Q

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • suresh shenoy

    char array Q

    I know that
    char s[100];
    s = "xyz";
    is invalid and should use char ptr or string library to store a string
    literal in s. Can anyone be precise why the second line is invalid?

  • vippstar@gmail.com

    #2
    Re: char array Q

    On Feb 20, 1:42 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
    suresh shenoy said:
    >
    I know that
    char s[100];
    s = "xyz";
    is invalid and should use char ptr or string library to store a string
    literal in s. Can anyone be precise why the second line is invalid?
    char s[100] = "xyz";
    >
    The contents of s are now 'x', 'y', 'z', and 97 '\0's.
    Just wanted to mention why this happends;
    In array initialization, when the initialization does not initialize
    all the elements of the array, the rest are given the value 0 (or NULL
    in pointer context)
    Notice, I said the rest; which means int foo[3] = { [1] = 42 };
    guarantees foo[0] to be 0.

    Comment

    Working...