Having trouble getting my head around declaring an array of C-strings(which themselves are arrays of chars). Any ideas?
Printable View
Having trouble getting my head around declaring an array of C-strings(which themselves are arrays of chars). Any ideas?
Like
char strings[howmany][howlongeachoneis];
Like:
charArray1[]
charArray2[]
charArray3[]
words[3]
words[0] = charArray1
words[1] = charArray2
words[2] = charArray3
(You have to fill in the types.)
If you wanted to access the 2nd letter of charArray3, you would do this:
charArray3[1];
But since words[2] ==charArray3, you can make that direct substitution and write:
words[2][1]
Or if you want to dynamically allocate the amount of memory you need for each string, use
Code:char *array1[10];
// allocate however much memory you need with new for each item
here's how i visualise what happens:
declare char array [5] [10];
here's what that looks like:
now cout<<array[2][3];Code:0 1 2 3 4 5 6 7 8 9
-------------------------------
0 | | | | | | | | | | |
-------------------------------
-------------------------------
1 | | | | | | | | | | |
-------------------------------
-------------------------------
2 | | | | x| | | | | | |
-------------------------------
-------------------------------
3 | | | | | | | | | | |
-------------------------------
-------------------------------
4 | | | | | | | | | | |
-------------------------------
will print out the value stored there, which is character x.
think of that as a co-ordinate system - first comes y-axis, then
x axis
>> Any ideas?
Switch to a vector of strings:Code:std::vector<std::string> myemptydata;
// or maybe
std::vector<std::string> tenstringsofdata(10);