4

Programming in C ( with -std=C89), and running into errors trying to pass a character string array into a function.

In main(), I've declared the array as follows:

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

In my_function, I've declared the array as:

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

I'm getting this error:

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

2
  • 1
    this link (eskimo.com/~scs/cclass/int/sx9a.html) should help you. Commented Apr 10, 2012 at 1:55
  • wow, that is a good link. thanks @Kunal! Commented Apr 10, 2012 at 14:47

3 Answers 3

3

A two-dimensional array of characters is still a character array and would have a prototype of char *my_array. So just change your function definition to this:

void my_function(char *my_array)

Note that this will flatten the array. There are different techniques to keep the two-dimensional-ness of the array, an easy way is to use this alternative prototype:

void my_function(char my_array[][COLS])

Which will preserve your array's dimensions when passed.

char **my_array means something completely different (pointer to an array, for example).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, If I change the prototype in main() to be void my_function( char *); and change the function definition to void my_function( char *my_array) I get this error: warning: passing argument 30 of 'fn_call_master_v1p0' from incompatible pointer type ../Project_Files/myheader.h:59: note: expected 'char *' but argument is of type 'char (*)[101]'
Thanks, If I change the prototype in main() to be void my_function( char *my_array[][COLS]); and change the function definition to void my_function( char *my_array[][COLS]) I get this warning: warning: passing argument 1 of 'my_function' from incompatible pointer type, note: expected 'char * (*)[101]' but argument is of type 'char (*)[101]'
I didn't say char *my_array[][COLS] but char my_array[][cols] (notice the extra * in your code). You can also use char (*my_array)[COLS], but there's no reason to do so.
2

You can pass a char[] variable as a char*, but you can't pass a char[][] as a char**. When you use the argument char** my_array, you are saying that *my_array has type 'pointer-to-char'. In reality, it has type 'array-of-char'. You would use an argument of type char** if you were using an array declared like char* x[]; and each element was a pointer to a dynamically-allocated buffer.

When working with multidimensional arrays, you have to remember that you can only replace the "innermost" dimension of array with *. If you try to abstract away more than one dimension, the compiler won't know how to do the array arithmetic. If you need a function that takes a multidimensional array with arbitrary sizes in all dimensions, then pass the array as a void*, pass the array dimensions as additional arguments, and then do all of the array arithmetic manually.

Comments

1

You can have a function signature with multi dimensional arrays, i.e.:

my_fun(char my_array[][COLS]);

You might get some out of this:

A Tutorial on Pointers and Arrays in C, see I.e. chapter 7.


Edit: I suspect you are trying to do something you do not 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;
}

Example output:

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 <---------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.