I am C beginner, trying to create dynamic list of the strings.
Initially I would like to have, for example:
char item[5][5];. {"car", "cow", "pen", etc...}
If I run out of space (rows) I want to do realloc to have char item[10][5];.
If I run out of characters I want to have char item[10][10]; etc..
Please somebody explain to me how to use malloc and realloc to do this.
I also have couple of issues listed below.
Here is my code:
The part I don't understand is why this does work:
1) This code executes without any errors. To me, it seems that it should produce some error since my char array is declared with char item[1][5];
2) Why would I use malloc to reserve space?
3) is it maybe that I am doing overflow but OS (Linux Fedora Core 2) doesn't know that I am overwriting who knows what and eventually program or computer will crash?
thanks in advance
Initially I would like to have, for example:
char item[5][5];. {"car", "cow", "pen", etc...}
If I run out of space (rows) I want to do realloc to have char item[10][5];.
If I run out of characters I want to have char item[10][10]; etc..
Please somebody explain to me how to use malloc and realloc to do this.
I also have couple of issues listed below.
Here is my code:
Code:
struct list {
int sz; //size
char item[1][5];
};
struct list mylist;
The part I don't understand is why this does work:
Code:
mylist.item[3][0] = 'x';
mylist.item[3][1] = 'y';
mylist.item[3][2] = '\0';
mylist.item[3][15] = 'a'; //this works also
printf("%s", item[3]); //I get "xy" printed
1) This code executes without any errors. To me, it seems that it should produce some error since my char array is declared with char item[1][5];
2) Why would I use malloc to reserve space?
3) is it maybe that I am doing overflow but OS (Linux Fedora Core 2) doesn't know that I am overwriting who knows what and eventually program or computer will crash?
thanks in advance