Passing multi-array pointer to function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    Passing multi-array pointer to function

    I forgot how to do this. I have a two-dimensional array[i][j] that I initialize a character at a time by looping through j, then increment i, repeat, so i should be an index to the j string. I then want to pass a pointer to this array to another function that can reference the strings by index i, like strcmp("mystrin g",*string[0]); or similar.

    I've tried every combination of pointer combination I can think of but it either doesn't work or generates errors. I'm not declaring variables/pointers correctly or I've just blinded myself to a solution.

    A little help please.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by drhowarddrfine
    I forgot how to do this. I have a two-dimensional array[i][j] that I initialize a character at a time by looping through j, then increment i, repeat, so i should be an index to the j string. I then want to pass a pointer to this array to another function that can reference the strings by index i, like strcmp("mystrin g",*string[0]); or similar.

    I've tried every combination of pointer combination I can think of but it either doesn't work or generates errors. I'm not declaring variables/pointers correctly or I've just blinded myself to a solution.

    A little help please.
    Something like this?

    [code=c]
    #define NOFSTRINGS 5
    #define STRLENGTH 40
    ...
    char strings[NOFSTRINGS][STRLENGTH+1]; // +1 for the terminating '\0'
    int i;

    // use string[i] here for the i-th string
    foo(strings[i]);
    ...

    void foo(char* string) {
    // etc.
    }
    [/code]

    kind regards,

    Jos

    Comment

    • drhowarddrfine
      Recognized Expert Expert
      • Sep 2006
      • 7434

      #3
      I just woke up. I believe that passes one of the strings to the function but I want the function to be able to access each string, individually.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by drhowarddrfine
        I just woke up. I believe that passes one of the strings to the function but I want the function to be able to access each string, individually.
        use strings[0], strings[1], strings[2] etc.

        kind regards,

        Jos

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          I thought I tried that already. Here's what I have:
          Code:
          	
          char strings[PATH_DEPTH][PATH_NAME_MAX];
          
          foo(page_name[PATH_NAME_MAX]);
          
          void order(char *page_name){
          printf("%s",page_name[1]);
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by drhowarddrfine
            I thought I tried that already. Here's what I have:
            Code:
            	
            char strings[PATH_DEPTH][PATH_NAME_MAX];
            
            foo(page_name[PATH_NAME_MAX]);
            
            void order(char *page_name){
            printf("%s",page_name[1]);
            }
            I don't know what you're doing there; the individual strings are strings[0] etc.
            PATH_NAME_MAX just is the maximum path length (string length) here and
            page_name[1] is a single character because page_name (as a parameter in
            your 'order' function) happens to be a char* (a pointer to zero or more characters).

            kind regards,

            Jos

            Comment

            • drhowarddrfine
              Recognized Expert Expert
              • Sep 2006
              • 7434

              #7
              Unless I'm misunderstandin g something,
              Code:
              char page_name[PATH_DEPTH][PATH_NAME_MAX];
              
              foo(page_name[i]);
              
              void foo(char *page_name){
              printf("%s",page_name);
              }
              This works and will make foo print the string at page_name[i] of the array, but I want foo to be able to select any of the strings in the array to print without relying on the calling function to pass each string (only a pointer to the whole array).

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by drhowarddrfine
                Unless I'm misunderstandin g something,
                Code:
                char page_name[PATH_DEPTH][PATH_NAME_MAX];
                
                foo(page_name[i]);
                
                void foo(char *page_name){
                printf("%s",page_name);
                }
                This works and will make foo print the string at page_name[i] of the array, but I want foo to be able to select any of the strings in the array to print without relying on the calling function to pass each string (only a pointer to the whole array).
                Ah, ok, you just have to change the type of foo's parameter then:

                Code:
                void foo(char[][PATH_NAME_MAX] page_name, int i) {
                   printf("String #%d= %s\n", i, page_name[i]);
                }
                Note that the column length must be known at compile time.

                kind regards,

                Jos

                Comment

                • drhowarddrfine
                  Recognized Expert Expert
                  • Sep 2006
                  • 7434

                  #9
                  I think you mean:
                  void foo(char page_name[][PATH_NAME_MAX] , int i)

                  That works and I swear I've done that multiple times before but it didn't work. I'm sure I was just messing things up going between two different files and changed something in the process. Thanks.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by drhowarddrfine
                    I think you mean:
                    void foo(char page_name[][PATH_NAME_MAX] , int i)

                    That works and I swear I've done that multiple times before but it didn't work. I'm sure I was just messing things up going between two different files and changed something in the process. Thanks.
                    Erm, yes, that's what I had in mind but the majority of my mind was still busy
                    programming in Java, sorry for the incorrect C syntax.

                    kind regards,

                    Jos

                    Comment

                    Working...