Hi,
How can i traverse a string array. for example, there is string array char** strings, and I just want to print every element (string) of this array.
Many thanks for your help!
Paul
Hi,
How can i traverse a string array. for example, there is string array char** strings, and I just want to print every element (string) of this array.
Many thanks for your help!
Paul
Just like you would any other array.
Code:for( x=0; x<y; x++ ) puts( z[ x ] );
Quzah.
Hope is the first step on the road to disappointment.
Hi quzah,
Thank you so much for your help.
Just a bit confused about y, what dose it refere to?
I'm quite newbi....
Paul
y is the number of elements in z[].
[edit]
And x is:
[/edit]Code:int x;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Not nessesarily. Might be more like sizeof(z)/sizeof(*z), assuming an array and not a pointer.
[edit]
Sorry, that's for generic arrays.
[/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Hi guys,
Thanks for the mesaages. They're really helpful.
Just want to know, if I don't know the size of the array, say, I've just got a char**, rather than char*[]. how can i set the loop?
Paul
You either have to know the size of it, or you have to set it up with a terminator so you'll know when to stop. It's up to you to decide how you stop. For example, maybe you've allocated all the space you need, plus one more pointer, which is set to NULL, to denote the end.
Quzah.
Hope is the first step on the road to disappointment.
You'll need to NULL-terminate the array or save its size, as I mentioned. [edit] And Quzah, too, when I was typing up my program. [/edit]Just want to know, if I don't know the size of the array, say, I've just got a char**, rather than char*[]. how can i set the loop?
Code:#include <stdio.h> #include <stdlib.h> void print(int **array, size_t size); int main(void) { int array[] = {1, 2, 3}; size_t x; for(x = 0; x < sizeof(array)/sizeof(*array); x ++) { printf("%i ", array[x]); } printf("\n"); print(array, sizeof(array)); return 0; } void print(int **array, size_t size) { size_t x; for(x = 0; x < size/sizeof(int); x ++) { printf("%i ", array[x]); } printf("\n"); }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
That's really cool.......
It's also really wrong as well
void print(int **array, size_t size);
is not the correct prototype for the array being passed.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Actually, what i did is as following,
It seems work...Code:char** aString // already assigned values by whatever way... int i = 0; while(aString[i] != NULL){ printf("%s", aString[i++]); }
Just wondering whether it would be wrong in some cases?
Paul
Assuming you've got one element set to NULL to stop your loop, you should be fine. If you don't want to have a spare element set to NULL, then you'll have to do what I did above and keep track of how many items you have total, stopping then.
Quzah.
Hope is the first step on the road to disappointment.
hi quzah,
Given a char**, is there any way that can find the number of elements in this char**?
Thanks!
Paul
No. Just like given a pointer you can't tell how many items it points to.
Quzah.
Hope is the first step on the road to disappointment.