anyone? lets say that I have an array of strings called str
how do I free all the values?Code:char** str;
anyone? lets say that I have an array of strings called str
how do I free all the values?Code:char** str;
and how do you find the length of a string array?
What do you mean by "length of a string array"? Do you mean the number of pointers in it? In this case, given a dynamic array, it's up to you to always always always always know how many items are in it.
To free, you would release each string separately, if you allocated the memory for them (if they're string literals or the like, don't free those), and then free str itself.
Yes I mean the number of char* pointers in it...
so say I have an array
hi | my | name | is
then the size of array is 4
Since you dynamically allocated the memory, you have to know. There is no way to recreate that information.
can I just do sizeof the array?
No, because str is just a pointer, and not an actual array. So you'll get 4 (probably).
Last edited by tabstop; 04-13-2008 at 06:14 PM. Reason: Forgot variable name
so we should keep track when we are doing a malloc in order to find the size of the array?
so say if I am given a char** str and someone else declared that array for me then there's no way that I can free it?
There's no way you can do anything at all with it. You can't even just print all the strings out, since there's no way to know how many there are.
Edit: We've used the words "linked list" before -- if you want/need to pass things around without keeping track of the size, that may be the way to go.
an accesing an array in C, can we do this:
int i = 9
array[i]?
Of course you can use a variable in the subscript of an array, to access the data. You can't use that to declare an array, but I know you've seen for-loops that iterate over an array using i.