C Program: newbie trying to pass 2D string array to function

C programming (with -std = C89) and doing errors trying to pass a character array of characters to a function.

In main()

I have declared an array like this:

#define ROWS 501
#define COLS 101
void my_function( char **);
...
char my_array[ROWS][COLS];
...
my_function(my_array);

      

In my_function

I have declared the array as:

void my_function( char **my_array )
{
...
}

      

I am getting this error:

warning: passing argument 1 of 'my_function' from incompatible pointer type, note: expected 'char **' but argument is of type 'char (*)[101]

+3


source to share


3 answers


A 2-D character array is still a character array and has a prototype char *my_array

. So just change your function definition to the following:

void my_function(char *my_array)

      

Note that this will flatten the array. There are different methods to preserve the two-dimensionality of an array, an easy way is to use this alternative prototype:



void my_function(char my_array[][COLS])

      

which will preserve your array sizes when passed.

char **my_array

means something completely different (a pointer to an array, for example).

+3


source


You can pass a variable char[]

as char*

, but you cannot pass char[][]

as char**

. When you use an argument char** my_array

, you say it *my_array

is of type 'pointer-to-char'. It is actually of type "array-of-w980>". You would use a type argument char**

if you were using an array declared as char* x[];

and each element was a pointer to a dynamically allocated buffer.



When working with multidimensional arrays, you must remember that you can replace the "innermost" size of the array with *

. If you try to abstract more than one dimension, the compiler won't know how to do the array arithmetic. If you need a function that accepts a multidimensional array with arbitrary dimensions in all dimensions, pass the array as void*

, pass the array dimensions as additional arguments, and then do all the array arithmetic by hand.

+2


source


You can have a function signature with multidimensional arrays, that is:

my_fun(char my_array[][COLS]);

      

You can get some of this:

For a tutorial on pointers and arrays in C , see Ie chapter 7.


Edit: I suspect you are trying to do something you don't need.

#include <stdio.h>

#define ROWS 501
#define COLS 101

char my_arr[ROWS][COLS];

void foo(char arr[][COLS])
{
    arr[44][23] = 'a';
    printf("foo_1:  %p\n", (void*) arr);
    printf("foo_2:  %p\n", (void*) &arr);
    printf("foo_3:  %p\n", (void*) arr[44]);
    printf("foo_4:  %p\n", (void*) &arr[44]);
}

int main(void)
{
    foo(my_arr);
    printf("my_arr[%03d][%03d] is %c\n", 44, 23, my_arr[44][23]);
    /* my_arr[44][23] is now 'a', (also here)  */

    printf("main_1: %p\n", (void*) my_arr);
    printf("main_2: %p\n", (void*) &my_arr);
    printf("main_3: %p\n", (void*) my_arr[44]);
    printf("main_4: %p\n", (void*) &my_arr[44]);

    return 0;
}

      

Output example:

foo_1:  0x804a040  <---+
foo_2:  0xbece91f0     |
foo_3:  0x804b19c  <--------+
foo_4:  0x804b19c  <--------+
my_arr[044][023] is a  |    |
main_1: 0x804a040 <----+    |
main_2: 0x804a040 <----+    |
main_3: 0x804b19c <---------+
main_4: 0x804b19c <---------+

      

+1


source







All Articles