Passing a 2d Array of pointers to a Function

This is a discussion on Passing a 2d Array of pointers to a Function within the C Programming forums, part of the General Programming Boards category; Hi, I have this array of pointers: char *test[50]; I want to be able to pass it to a function ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Passing a 2d Array of pointers to a Function

    Hi, I have this array of pointers:

    char *test[50];

    I want to be able to pass it to a function without the function being able to change anything in the array. Any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You probably want the function to make a copy of the array. Alternatively, you could write

    Code:
    struct S {
            int A[100];
    };
    
    void f(S s)
    {
    
    }
    Some compilers have been known to crash when passing large structures by value like this.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Why not using const?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Micko
    Why not using const?
    Here's why.

    That and that fact that it doesn't really work:
    Code:
    #include <stdio.h>
    
    void bar( const int * const foo[] )
    {
            int x = 0;
    
            /* This compiles and works as it should. */
            for( x = 0; x < 5; x++ )
            {
                    printf( "*foo[%d] is %d\n", x, *foo[x] );
            }
    
            for( x = 0; x < 5; x++ )
            {
                    printf( "trying to change *foo[%d] to %d\n", x, x*x );
                    *foo[x] = x*x;
            }
    
            for( x = 0; x < 5; x++ )
            {
                    printf( "*foo[%d] is %d\n", x, *foo[x] );
            }
    
            for( x = 0; x < 5; x++ )
            {
                    printf( "trying to change foo[%c] to &x\n", x );
                    foo[x] = &x;
            }
    
            for( x = 0; x < 5; x++ )
            {
                    printf( "*foo[%d] is %d\n", x, *foo[x] );
            }
    }
    
    
    int main( void )
    {
            int a = 10, b = 20, c = 30, d = 40, e = 50;
            int *foo[5] = { &a, &b, &c, &d, &e };
            int x;
    
            bar( foo );
    
            for( x = 0; x < 5; x++ )
            {
                    printf("*foo[%d] is %d\n", x, *foo[x] );
            }
            
            return 0;
    } 
    
    [edit]
    Here's the warnings if anyone cares:
    :~/programming$ gcc -o constc constc.c -Wall
    constc.c: In function `bar':
    constc.c:16: warning: assignment of read-only location
    constc.c:27: warning: assignment of read-only location
    constc.c: In function `main':
    constc.c:42: warning: passing arg 1 of `bar' from incompatible pointer type
    [/edit]
    
    *foo[0] is 10
    *foo[1] is 20
    *foo[2] is 30
    *foo[3] is 40
    *foo[4] is 50
    trying to change *foo[0] to 0
    trying to change *foo[1] to 1
    trying to change *foo[2] to 4
    trying to change *foo[3] to 9
    trying to change *foo[4] to 16
    *foo[0] is 0
    *foo[1] is 1
    *foo[2] is 4
    *foo[3] is 9
    *foo[4] is 16
    trying to change foo[] to &x
    trying to change foo[] to &x
    trying to change foo[] to &x
    trying to change foo[] to &x
    trying to change foo[] to &x
    *foo[0] is 0
    *foo[1] is 1
    *foo[2] is 2
    *foo[3] is 3
    *foo[4] is 4
    *foo[0] is 5
    *foo[1] is 1075140144
    *foo[2] is 1075140144
    *foo[3] is 1075140144
    *foo[4] is 1075140144
    


    Quzah.
    Last edited by quzah; 09-11-2004 at 04:10 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    When I try to compile your program I get comipler errors:
    on lines:
    Code:
    ....
     for( x = 0; x < 5; x++ )
            {
                    printf( "trying to change *foo[%d] to %d\n", x, x*x );
                    *foo[x] = x*x;//here
            }
    ...
    
     for( x = 0; x < 5; x++ )
            {
                    printf( "trying to change foo[%c] to &x\n", x );
                    foo[x] = &x;//here
            }
    ...
    And errors are:
    error C2166: l-value specifies const object

    I've learned c on visual studio (first 6.0 then .net) so maybe that is the reason of errors. I always save my files as .c when I want strict C. Also when I try to compile this with Turo C 2.01 I get pretty much same errors something like Cannot modify const object in function....

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well GCC just gives warnings, not errors. I would prefer it give you errors, and not compile. But, as stated, GCC will compile it and allow it ro run.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    IMO its not a defect of GCC that it only warns on that; sometimes one needs to compile old crusty code that takes certain liberties; and when you're writing your own code you should pay attention to all warnings anyway. I believe one can use -Werror or similar to make the all warnings errors too.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. passing 2d array in to function
    By Kings in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 10:35 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. 2D array passing to function
    By mkorolen in forum C Programming
    Replies: 5
    Last Post: 03-31-2002, 07:37 PM

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