help with passing 2d array...

This is a discussion on help with passing 2d array... within the C Programming forums, part of the General Programming Boards category; hi, i just have a quick question. i have a 2d array declared like: Code: char array[10][6]; This array contains ...

  1. #1
    Unregistered
    Guest

    help with passing 2d array...

    hi,

    i just have a quick question. i have a 2d array declared like:
    Code:
    char array[10][6];
    This array contains 10 strings. each are 5 characters long. i also have a funciton that is defined as taking an argument of type char**. Is there any way i can pass the array to this function. i've tried, but i get typing errors, and when i cast the type, the program seg faults...explinations would really help me understand this...

    thank you.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Can you post you're code so we can take a look at it?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    The easiest thing to do is to redefine your function to accept a 2D array, not a pointer to a pointer.

    Examples
    Code:
    #include <stdio.h> 
    
    void foo ( char **p ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf( "%s\n", p[i] );
      }
    }
    
    void bar ( char p[][5] ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf( "%s\n", p[i] );
      }
    }
    
    int main ( ) {
      char test[][5] = {
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
      };
      char *p[10];
      int i;
      for ( i = 0 ; i < 10 ; i++ ) p[i] = test[i];  // convert from [][] to **
      foo( p );
      bar ( test );
      // foo( test );	// this is a no-no
      return 0;
    }

  4. #4
    Unregistered
    Guest
    shiro, here's some sample code. The real code is too cumbersome (cryptic typdefs and such) to post, but you should get the idea from this.
    Code:
    #include <stdio.h>
    
    void foo (char**);
    
    int main ()
    {
        char array[10][6] = { "fj324", "fj349", "fj239", "fd349", "fj239",
                              "fj233", "fd234", "fj239", "fj239", "fj342" };
        foo (array);  // doesn't work
        return 0;
    }
    
    void foo (char** array)
    {
        int i = 0;
        while (i < 10) {
            printf ("%s\n", array[i]);
            i++;
        };
    }
    Salem, i just saw your reply, and you've answered my question. Thank you...however i confused as to why you can't pass the array as a pointer. Isn't a 2d array, which contains strings, the same as a pointer to a pointer. Maybe not. i just need clarification on why it doesn't work...thank you...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Isn't a 2d array, which contains strings, the same as a pointer to a pointer
    No

    A 2D array is a contiguous block of memory starting at a[0][0] and finishing at a[x][y]

    With a pointer to a pointer, only each level of indirection is contiguous - p[0] is followed by p[1], and p[0][0] is followed by p[0][1].
    But there is no relationship between say p[0][0] and p[1][0].

    Try drawing them out on paper - you'll see that they are quite different.

    Although the compiler will allow you to reference either using array notation, it generates different code depending on whether the declaration is [][] or **, and this has to be consistent with what is really being passed to the function.

  6. #6
    Unregistered
    Guest
    thank you, sir...that makes perfect sense...

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    you dont really have to make the function accept it, unless you need to work with every element/char in the array. just pass it like string[0] or whatever. this would be the first string of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. passing 2D array of ptrs as parameter
    By Psycho in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2003, 10:13 AM
  4. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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