Thread: Passing two-dimensional arrays to functions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Passing two-dimensional arrays to functions

    Hello everyone, I'm trying to send a non-const two-dimensional array to a function that receives it as const 2-dimensional array, but the compiler (GNU GCC Compiler) issues the following warning in the function call:

    " warning: passing argument 1 of 'receiveArray' from incompatible pointer type ".

    Also, it issues the following warning in the function prototype:

    " note: expected 'const int (*)[2]' but argument is of type 'int (*)[2]' ".

    The code is:

    Code:
    #include <stdio.h>
    
    /* Function prototype: */
    void receiveArray(const int array_copy[][2]);
    
    int main(void)
    {
        int array[2][2] = { { 2, 1 }, { 1, 4 } };
    
        receiveArray(array);
    
        return 0;
    }
    
    void receiveArray(const int array_copy[][2])
    {
        printf("Array received successfully.\n");
    }
    If I declare array as const int, the program is compiled without warning - so, is there a way to send a non-const double-subscripted array to a function and that function to receive it as const? I know it is possible with 1-dimensional arrays. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    I actually tried compiling the code you provided and it worked without any problems. Not sure exactly why ? I compiled with -Wall on gcc.

    EDIT: was using code::blocks and mingw on winows, upon switching to Linux terminal and gcc it gives the error. Not sure what's up. Hopefully someone else will know.
    Last edited by Ocifer; 02-04-2012 at 09:24 PM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you should change your function so you can pass in what you need to pass. The array_copy variable is a pointer to an array so that is why it is treated differently than what you're used to. If the compiler didn't give this warning, it would mean that you could use constless pointers to modify const variables.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by Ocifer View Post
    I actually tried compiling the code you provided and it worked without any problems. Not sure exactly why ? I compiled with -Wall on gcc.

    EDIT: was using code::blocks and mingw on winows, upon switching to Linux terminal and gcc it gives the error. Not sure what's up. Hopefully someone else will know.
    Thanks for testing. I am using Code::Blocks with mingw too, in Windows XP - is it possible that there might be something wrong with my computer?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by whiteflags View Post
    I think you should change your function so you can pass in what you need to pass. The array_copy variable is a pointer to an array so that is why it is treated differently than what you're used to. If the compiler didn't give this warning, it would mean that you could use constless pointers to modify const variables.
    Thanks, whiteflags, but how is it possible that I can make the passing I'm trying to make with 1-dimensional arrays?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    To stdq, I doubt there's anything wrong with your computer. You're just not understanding const to to its fullest implications, though to be honest neither am I. Your question reminds me I have much more to study.

    However, like whiteflags suggested, just alter the functions to accept what you want. Also, when writing functions that deal with arrays, I usually try to write the functions so that they only change what I want them to change. I assume you're using const to try to keep the declared array unchanged. Maybe I'm being a bit simplistic (would appreciate being corrected if I'm wrong), but if you don't want to change the array just don't let it appear in the left-hand side of an assignment within the function. What are you trying to do with the array once you get it into the function? If all you need is to use values FROM the array and not alter any of them, it is possible to do this without the const construct.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Hi, Ocifer, thanks for the help. I'm not trying to change the array within the function, but this doubt came up when I was doing another program larger than the one I've written in here. Anyway, I'll try to find out more about the const qualifier.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by stdq View Post
    Thanks, whiteflags, but how is it possible that I can make the passing I'm trying to make with 1-dimensional arrays?
    I managed to find a discussion elseweb on this exact problem and I found your answer:

    Not a bug. The function parameters are of type "pointer to array[4] of const double" because const on an array type applies to the element type, recursively, and then the outermost array type, only, of a parameter of array type decays to a pointer, and the arguments passed are of type "pointer to array[4] of double" after array-to-pointer decay, and the only case where qualifiers are permitted to be added in assignment, argument passing etc. is qualifiers on the immediate pointer target, not those nested more deeply.
    In other words, you're allowed to add const to a pointer type, but a const int (*)[2] involves more levels of indirection. You're not allowed to modify the constness of a pointer with more than one level of indirection, by the rules of the language. The rule deals with the issue of breaking const, which I touched upon earlier.
    Last edited by whiteflags; 02-05-2012 at 10:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  2. Passing 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  5. Passing 2 dimensional Arrays to functions
    By aresashura in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2001, 12:59 AM