traverse a string array

This is a discussion on traverse a string array within the C Programming forums, part of the General Programming Boards category; Hi, How can i traverse a string array. for example, there is string array char** strings, and I just want ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    traverse a string array

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    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

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    y is the number of elements in z[].

    [edit]
    And x is:
    Code:
    int x;
    [/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.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    sizeof(z)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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?
    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]
    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.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    That's really cool.......

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Actually, what i did is as following,

    Code:
    char** aString // already assigned values by whatever way...
    
    int i = 0;
    
    while(aString[i] != NULL){
        printf("%s", aString[i++]);
    }
    It seems work...

    Just wondering whether it would be wrong in some cases?

    Paul

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    hi quzah,

    Given a char**, is there any way that can find the number of elements in this char**?

    Thanks!

    Paul

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21