Thread: Am I right?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Question Am I right?

    Here's a question I was asked. Am I right so far?

    10. Indicate using ticks and crosses which of the following string variable declarations are valid and self-consistent.

    char s[]=”Hello”; Yes
    char s[]; Yes
    char s[6]; Yes
    char s[5]=“Hello”; No
    char *s=”Hello”; No

    Please let me know if I'm right or wrong and what the hell the last one is supposed to be ?

    Cheers.
    Gaz.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > char *s=”Hello”;

    This is actually correct. This is valid. "char *s" means:

    "s is a pointer to a character", or "a character pointer named s"

    It is legal to do that assignment.
    Keep in mind though that "Hello" becomes a string literal.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    char s[]="Hello"; Yes
    char s[]; No /*What will you do with it?*/
    char s[6]; Yes
    char s[5]="Hello"; No /*Run Time Error*/
    char *s="Hello"; Yes
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by zahid
    char s[]; No /*What will you do with it?*/
    char s[5]="Hello"; No /*Run Time Error*/
    [/B]
    Allow me to elaborate on why neither of these declarations are valid.

    char s[] tells the compiler you want to create an array of characters (a string), but it does not tell the compiler how much memory to reserve for the array. The compiler must know how much memory to set aside for your array, so this declaration fails. char s[]="Hello" works because of the initilization. The compiler knows what you want to put into the array (the characters 'H', 'e', 'l', 'l', 'o', '\0'), and sets aside enough memory.

    char s[5]="Hello" might look fine to you. After all, the array 's' is five characters in length and the word "Hello" only requires five characters, so what's the problem? In C, strings are terminated with a null zero (the character '\0'). The null zero is a character, so it takes up just as much space as the 'H' or the 'e' in "Hello". char s[5] does not leave enough room for the null zero, so you are trying to initialize the array by putting something too large into it. Either of the following will work here:
    Code:
    char  s[]="Hello";
    char  s[6]="Hello";
    Jason Deckard

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char s[5]="Hello" might look fine to you.
    That's because it is fine (as far as C is concerned)

    The compiler drops the \0 when presented with this particular kind of initialisation.

    The 'problem' is that such an array cannot be used with some of the standard library string functions - but that's a different question.

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Salem
    The 'problem' is that such an array cannot be used with some of the standard library string functions - but that's a different question.
    Originally posted by DjGazUK
    10. Indicate using ticks and crosses which of the following string variable declarations are valid and self-consistent.
    I believe this makes it an invalid string (a perfectly good character array, but not a string). I suggest the student cross out char s[5]="Hello"; as invalid.
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Thanx Jason

    Thanx jason.

    You have been a star mate

Popular pages Recent additions subscribe to a feed