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

This is a discussion on pass a 2d array to function which takes an array of pointers as argument? within the C Programming forums, part of the General Programming Boards category; 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]; ...

  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
    32,845
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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. Make an array of function pointers
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-09-2003, 06:29 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 09:37 PM

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