Thread: pass a 2d array to function which takes an array of pointers as argument?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    pass a 2d array to function which takes an array of pointers as argument?

    So what I want to do is something like that:

    Code:
    void function(char *ar[]){
        strcpy(ar[0],"blablabla"); 
    } 
    int main(){ 
        char str[100][200]; 
        function(str);       //wrong
    }
    I mean I want to have a function to which i can pass a 2d array and it ll make some changes to it. I do not want a function with a 2d array argument cause i do not want to set the size of array explicitly, so I believe this is the only way. The thing is that

    function(str);

    is wrong and I get a warning during compile:

    test.c:9: warning: passing argument 1 of function from incompatible pointer type

    so what I 'm asking is, what is the right way to call function on this array or/and if there is another way to do all this.
    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    void function(char ar[][200])
    is all you need.

    > I do not want a function with a 2d array argument cause i do not want to set the size of array explicitly
    Tough luck for you then.
    http://c-faq.com/aryptr/ary2dfunc2.html
    Whilst you can do it by this trick, you lose the true 2D array subscripting in the process and you have to do it all by hand.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  3. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  4. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM