suppose I had
char * lines[50];//create 50 char pointers
how can I increase to 100 or more char pointers?
Printable View
suppose I had
char * lines[50];//create 50 char pointers
how can I increase to 100 or more char pointers?
At run time, or compile time? At run time, you can't, because you hard coded the size. (Not using it that way anyway.)
At compile time? Just change the 50 to 100 or more.
At run time, not using an array? Search for "dynamic 2D array". :wink:
Quzah.
I want a array list with pointers
like [0]-> -> -> ->
[1]-> -> -> ->
[2]-> -> -> ->
.
.
.
which can increase itself when it reached its limit
what shall I do? will malloc do it?
C doesn't do that. If you want to change it's size you have to do it yourself.
Yes you can create the pointer array with malloc() at almost any size you like.Quote:
what shall I do? will malloc do it?
You can change it's size with realloc() if you need more space or to release unused elements.
HOWEVER... this method also requires you to malloc() every string you point to from the base array.
It all ends up in dynamic memory and you have to remember to free() it all when you're done.
Would you mind to show me a example of mallocing pointer array?Quote:
Yes you can create the pointer array with malloc() at almost any size you like.
You can change it's size with realloc() if you need more space or to release unused elements.
HOWEVER... this method also requires you to malloc() every string you point to from the base array.
It all ends up in dynamic memory and you have to remember to free() it all when you're done.
char **pStrings = malloc(NumberOfElements * sizeof(char*));// does this line means to create space for NumberOfElements of char pointers?
Exactly. It creates an array of pointers... each element points to a char array that you also create with malloc.
One of the big advantages of this technique is that each of the char arrays holding your strings can be different sizes where as the standard char string[50][50] creates an array where all strings are the same size.