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

1. 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. array[N][M]. You have a 2D array of N rows and M columns. How should you picture that?

In the pic N = 3 and M = 4.

3. ^ 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. 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!!).

5. ^ 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. 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!

7. Originally Posted by Rubik
^ 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