Thread: ! C Question: Passing multi dimensional arrays through routines

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    ! C Question: Passing multi dimensional arrays through routines

    hello all, i have a c question.

    This is in regards to passing a multi dimensional array through to another routine.

    i have defined in one routine, a multi 2-dimensional array:


    byte sitecode_rules[10][15];


    and i am using it as so:


    if( check_code_rules( sitecode_rules, scan_delivery, s_count ) )


    compiler gives me a suspicious pointer conversion on that line.

    the check_code_rules routine looks something like this:


    byte check_code_rules( byte *rule[], byte *scan, int number_of_rules )
    {
    ...
    }

    I am working on an embedded platform and the rule[] array data is somehow getting corrupt within the check_code_rules routine.

    I know this because i output the data within the array on the lcd.
    The data is ok however, within the higher routine which calls check_code_rules.

    I have tried

    if( check_code_rules( &sitecode_rules[0], scan_delivery, s_count ) )

    and even,

    if( check_code_rules( &sitecode_rules[0][0], scan_delivery, s_count ) )

    data is still corrupt.

    What am I doing wrong?

    (calling all c gurus)
    Last edited by aaronc; 04-26-2004 at 09:26 PM.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    to pass 2 dimensional arrays you prototype is as[code]
    void func(char **2darray);
    or
    void func(char *2darry[]);
    so something must be wrong with your function

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    thanks for your reply linuxdude

    so you are saying that when i'm using the function, the following should work?


    if( check_code_rules( sitecode_rules, scan_delivery, s_count ) )


    Because the compiler is complaining about a Suspicious pointer conversion on that line.


    if( check_code_rules( &sitecode_rules[0], scan_delivery, s_count ) )

    and

    if( check_code_rules( &sitecode_rules[0][0], scan_delivery, s_count ) )

    gives the same warning from the compiler.

    it's only a warning but the data within the array is corrupt somehow, the routine itself does not do anything with the data. I'm only outputting it on the lcd.
    Last edited by aaronc; 04-26-2004 at 09:31 PM.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    post all your code and let us see and also post the exact compiler warning please(to help you of course) and plz use [ code] and [ /code] tags without the spaces
    Last edited by linuxdude; 04-26-2004 at 09:32 PM.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    byte sitecode_rules[10][15]; 
    byte *rule[]
    The first line declares a two dimension array of bytes. The second one declares a one dimensional array of pointers to bytes. Therefore, they are not interchangeable.

    If you want to pass a two dimension array to a function, you must provide the size of the second dimension.
    Code:
    byte check_code_rules(byte rule[][15], byte *scan, int number_of_rules )
    This thread may help you:
    http://cboard.cprogramming.com/showthread.php?t=51608

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    cheers, that fixed it.

    ta anonytmous

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To clarify, you have to provide the size of all dimensions execpt the first. It is optional:
    Code:
    void foo( int bar[] ); /* fine */
    void fOo( char bar[][10] ); /* fine */
    void foO( char bar[10][] ); /* not fine */
    void Foo( char bar[][3][4] ); /* fine */
    void FOo( char bar[3][4][5] ); /* fine */
    And so on...

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  4. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM