I was wanting to create a 2 dimensional string array that had dimensions “string[5][*]” but was having some trouble. I would like to do something like this:
...
for(i = 0;i < 5;i++){
char* word = ...;
if(strcmp(word,...)){
string[i][j] = (char *) malloc(/*size of word*/);
string[i][j] = word;
j++;
}
}
The “string” variable should be of length 5 in the ith direction and should be allowed to grow as much as I need in the jth direction.
I tried doing:
char* string[5];
but that does not seem to work. Please note the above for loop has been simplified and parts might not seem logical.
EDIT:
I am trying to sort a list of strings into 5 categories. So the n-dimentional array should in a sense hold 5 arrays containing an unspecified number of strings (not chars). This I assume is a 3-dimentional char array but was having trouble thinking of how to write it. I want every string[i][j] term to hold a char array. So string[0][0] could equal “cat” or something.
Try this.
Also in your code at the top the problem is each
string[i][j]is acharand you can’t malloc for a non pointer. Everystring[i]is achar *you have to malloc for that.