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]
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).
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.
You can have a function signature with multi dimensional arrays, ie:
my_fun(char my_array[][COLS]);
You might get some out of this:
A Tutorial on Pointers and Arrays in C , see Ie 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 <---------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.