Can strings fit in an array? (2 beginner questions)

This is a discussion on Can strings fit in an array? (2 beginner questions) within the C Programming forums, part of the General Programming Boards category; 1. Hello! I'm new to C. I finished reading a beginning C book, and in the section about arrays, it ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    23

    Can strings fit in an array? (2 beginner questions)

    1. Hello! I'm new to C. I finished reading a beginning C book, and in the section about arrays, it says that one string can fit in a character array (char arrayname[]) but there cannot be a string array (string arrayname[]) that have multiple strings. Is that true?

    Is
    Code:
    string arrayname[4] = {"one", "two", "three"};
    not valid? My compiler lets me run it and it works, but why is the book saying it's wrong?

    2. I know you can represent multiple strings in a character array by:
    Code:
    char newarray[10][4] = ("one", "two", "three");
    because [10][4] indicates that there should be four newarrays created with a max of 10 characters each, but is
    Code:
    string multiplestrings[10][4] = ("i love you", "hello come to me", "i don't get C"; "hello world", "what are arrays"; "i am happy", "I am learning how to code");
    valid? Does multiplestrings[10][4] basically create 4 string arrays that have a maximum of 10 different strings within each string array?

    Thank you!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    array[N][M]. You have a 2D array of N rows and M columns. How should you picture that?
    Name:  str.jpg
Views: 116
Size:  14.3 KB

    In the pic N = 3 and M = 4.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    ^ Thank you for answering. One clarification though: Are you basically saying that

    string multiplestrings[10][4] = ("i love you", "hello come to me", "i don't get C"; "hello world", "what are arrays"; "i am happy", "I am learning how to code");

    is valid, and i can use that as code? Thank you!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    N = 10. M = 4. Well you use string, which makes the whole thing wrong (string is a char *), so you have something like a 3D array now. Not what you want.
    Let's say you use
    Code:
    char array[10][4]
    then you can store 10 strings that has -at most- (4-1)=3 characters. (-1 for the null terminator!!).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    ^ Can I use
    string array[10][4] to store 10 string arrays with 4 strings in each array?

    (Like for example: string multiplestrings[10][4] = ("i love you", "hello come to me", "i don't get C"; "hello world", "what are arrays"; "i am happy", "I am learning how to code"); )?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are going to make your life harder. Use
    Code:
    char array[10][4]
    This array can hold 10 strings, each one of maximum length 3!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Rubik View Post
    ^ Can I use
    string array[10][4] to store 10 string arrays with 4 strings in each array?
    What's "string"? There is no type called "string" in C.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some beginner questions.
    By Kitt3n in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2010, 05:18 PM
  2. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 05:29 PM
  3. Beginner's Questions
    By bjl in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2008, 06:56 AM
  4. several beginner questions...
    By Taikon in forum C Programming
    Replies: 2
    Last Post: 02-09-2005, 09:53 AM
  5. 2 beginner questions
    By GCat in forum C++ Programming
    Replies: 15
    Last Post: 11-24-2004, 03:55 AM

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