Passing 2 dimensional array to a function

This is a discussion on Passing 2 dimensional array to a function within the C Programming forums, part of the General Programming Boards category; Hi guys, How can I pass a 2 dimensional array to a function using [] and pointer *? Code: void ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Passing 2 dimensional array to a function

    Hi guys,

    How can I pass a 2 dimensional array to a function using [] and pointer *?

    Code:
    void InputString(????)  // Emp[][] ,  **Emp  ??
    {
    }
    
    int main(void)
    {
    	char cEmployee[5][20];
    	int i;
    
    	for (i=0;i<5;++i)
    	{
    		puts("Enter name:");
    		InputString(??????);
                    }
                   return 0;
    }
    Many thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If i remember correctly, this should do it:
    Code:
    void foo(char* array)
    {
    
    }
    
    int main()
    {
        char array[10][10];
        foo(array);
    }
    Basically you just pass the pointer to the first element in the array.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I dont think that will work. That only works on one dimensional arrays. It cannot work on 2 dimensional arrays because it would need to know the size of at least one of the dimensions in order for it to be able to calculate the position of the element.

    Havnt tried but maybe this:

    Code:
    void InputString(char array[5][])
    {
    }
    If you've ever tried to simulate a 2 dimensional array using just one dimension it might of looked like this:

    Code:
    const int width = 5;
    const int height = 10;
    int myArray[width * height];
    ...
    ...
    int someElement = myArray[4 * width + 3]; //like myArray[3, 4];
    It need to know to know the "width" of the array in order for it to calculate its single dimension position.
    Last edited by 39ster; 01-14-2008 at 06:09 PM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The easiest way is to just make the function parameter look like the same type you're passing to it, then just pass the array:
    Code:
    void InputString(char employee[5][20]) {}
    /* ... */
    char cEmployee[5][20];
    InputString(cEmployee);
    As has been hinted, there are multiple ways of writing InputString that are the same:
    Code:
    void InputString(char employee[5][20]);
    void InputString(char employee[][20]);
    void InputString(char (*employee)[20]);
    Why these are all equivalent is probably a matter best left for later in your C education. For now, just pick one: the one listed second seems to be most common, in my experience.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    They are not the same.

    http://c-faq.com/aryptr/index.html

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need to pass the function the size of the array's first dimension, but you may (the compiler will ignore it). The last dimension size, you must pass though.

  8. #8
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Quote Originally Posted by robwhit View Post
    They are not the same.

    http://c-faq.com/aryptr/index.html
    If this is in reply to my post, please explain how those function prototypes differ; I'm quite sure they don't.

    If it's not, then ignore this.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You can use pointer arithmetic with the pointer version, not with the others.

    The 2nd one has an unspecified number of elements, the first one has a specifed number of elements. That means that if you enter in an invalid index constant in the first one, you'll probably get a compile error.

    And whatever other differences are in the link.
    Last edited by robwhit; 01-15-2008 at 09:30 PM.

  10. #10
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Quote Originally Posted by robwhit View Post
    You can use pointer arithmetic with the pointer version, not with the others.

    The 2nd one has an unspecified number of elements, the first one has a specifed number of elements. That means that if you enter in an invalid index constant in the first one, you'll probably get a compile error.

    And whatever other differences are in the link.
    They're all the same; note that these are prototypes, and as a parameter of a prototype, what looks like an array is really a pointer (this is not the case elsewhere, of course).

    You can indeed do pointer arithmetic with the parameter of all three methods because they are identical. You can do pointer arithmetic on arrays, too (because arrays decay into pointers in most cases), but I presume you mean something like employee++? That's still possible with all the prototypes I showed due, again, to the fact that arrays aren't really arrays (in this particular context).

    Here's a demonstration:
    Code:
    #include <stdio.h>
    
    static void f1(int *p)
    {
      printf("%d\n", *++p);
    }
    
    static void f2(int p[])
    {
      printf("%d\n", *++p);
    }
    
    int main(void)
    {
      int a[] = { 1, 2 };
    
      f1(a);
      f2(a);
    
      return 0;
    }
    I used a single array for simplicity; this naturally holds for multidimensional arrays, but it gets slightly more complicated because you start to deal with pointers to arrays.

    You can, perhaps, further convince yourself with this:
    Code:
    #include <stdio.h>
    
    static void f(char p[11])
    {
      printf("In f(): %lu\n", (unsigned long)sizeof p);
    }
    
    int main(void)
    {
      char a[11];
    
      printf("In main(): %lu\n", (unsigned long)sizeof a);
      f(a);
    
      return 0;
    }
    Unless you're on an absolutely bizarre system, two different numbers should come out: 11 in main() (this won't change regardless of platform), and probably 4 in f() (depending on the size of a pointer).

    Despite the fact that it looks like an array, inside the function f(), p is a pointer.

    This is covered in section 6.4 of the FAQ link you posted.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Dang, you're right. It even keeps the value assigned and does no type check whatsoever on the size.

    Even this prints 4:
    Code:
    #include <stdio.h>
    typedef int arr[5];
    
    static void f1(arr p)
    {
            typeof (p) arr2;
            printf("%lu\n", (long unsigned) sizeof arr2);
    }
    
    int main(void)
    {
            int a[3] = { 1, 2 };
            f1(a);
    
            return 0;
    }
    I thought that they worked like arrays and that the FAQ was just keeping things simple or something.

    I really should use my compiler more...

    Thanks for pointing this out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Replies: 4
    Last Post: 11-07-2001, 01:46 PM

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