string array question if I get it?

This is a discussion on string array question if I get it? within the C Programming forums, part of the General Programming Boards category; Hi, this is my code: Code: #include <stdio.h> int main(){ char message[12]; printf("Hit me with max. 10 hits!"); fgets(message, 12, ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Red face string array question if I get it?

    Hi,
    this is my code:
    Code:
    #include <stdio.h>
    
    int main(){
            
        char message[12];
    
        printf("Hit me with max. 10 hits!");
        fgets(message, 12, stdin);
    
        printf("What you hitted:\n%s", message);
    
        return 0;
    }
    I have read that an array of string also includes \0 and \n and I also read that fgets adds an \n.
    So is my idea in the table right?
    index
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    value 1 2 3 4 5 6 7 8 9 0 \n \0 \n

    I am looking forward to hear from you.

    King regards,
    Greenality

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    \0 must be present at the end. \n can be present in the string, just as with any other character. Input from fgets may have an \n at the end, depending on how many characters the user typed.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    A string is just a character array with a terminating character ('\0') at the end.

    Code:
    // explicitly initialize each element of the array
    char array_1[6] = {'H', 'e', 'l', 'l', 'o', '\0' };
    
    // initialize the array with a string literal (the compiler will automatically
    // add the null character, but it's up to the programmer to leave enough space
    // for it in the array!
    char array_2[6] = "Hello";
    "fgets()" reads in a string and terminates it, but it has the caveat of also storing the newline if there is room.

    So to correct your example:

    Code:
    index  0 1 2 3 4 5 6 7 8 9 10 11
    value  1 2 3 4 5 6 7 8 9 0 \n \0
    Note that, in my correction, index only goes up to 11. That is because the valid range of "array[n]" is from zero to "n-1".

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    Ok, thank you for your help and for your really quick answers. :-) Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about string & character array
    By Roy01 in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2006, 09:50 AM
  2. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  3. String array question
    By Vanished in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 06:27 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM
  5. Array/string question
    By Lyra in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:47 PM

Tags for this Thread


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