Unable to pass an address of array of type char *[2] to function taking char ***

bwoebi

My XCode (default compiler should be clang?) shows me on this code a warning:

Incompatible pointer types passing 'char *(*)[2]' to parameter of type 'char ***' (when calling func)

void func (char ***arrayref, register size_t size) {
    /// ...
}

int main()
{
    char *array[2] = {"string", "value"};
    func(&array, 2);
}

while this code is no problem (=no warning):

void func (char **arrayref, register size_t size) {
    /// ...
}

int main()
{
    char *array[2] = {"string", "value"};
    func(array, 2);
}

While this removes the warning

func((char***)&array, 2);

I still don't know why the first emits a warning, while the latter doesn't.

Also, when the first is a problem, I'd also expect that the first emits a warning like:

Incompatible pointer types passing 'char *[2]' to parameter of type 'char **'

John Bode

Time for a brief refresher on array semantics.

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.

The expression array in your code has type "2-element array of char *", or char *[2]. When you pass it as an argument to func, as in

func( array, 2 );

the expression is converted to an expression of type "pointer to char *", or char **, which is the type your function is expecting, and the value of the expression is the address of the first element: array == &array[0]. This is why you don't get the warning for the second code snippet.

In the first snippet, the array is an operand of the unary & operator, so the conversion to a pointer type doesn't happen; instead, the type of the expression is "pointer to 2-element array of char *", or char *(*)[2], which is not compatible with char **. The address value is the same (the address of the first element of the array is the same as the address of the array itself), but the types don't match, hence the warning.

So why does this matter? A pointer is just an address, and all address are the same, right? Well, no. A pointer is an abstraction of an address, with associated type semantics. Pointers to different types don't have to have the same size or representation, and pointer arithmetic depends on the type of the pointed-to type.

For example, if I declare a pointer as char **p;, then the expression p++ will advance the pointer to point to the next object of type char *, or sizeof (char *) bytes from the current address. If p is declared as char *(*p)[2], however, the expression p++ will advance p to point to the next two-element array of char *, which is 2 * sizeof (char *) bytes from the current address.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass an array of Swift strings to a C function taking a char ** parameter

From Dev

how to pass 2D array type char (string) as function parameter?

From Dev

Pass char pointer array to function

From Dev

Pass char pointer/array to a function

From Dev

Pass a 2D char array to a function in C

From Dev

How to pass an array of char's to a function in C

From Dev

Can't pass a char array to a function

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass in function a multidimensional char array in C?

From Dev

pass and modify char array[][] in function C

From Dev

How to pass char into function?

From Dev

Why address is type casted to char *?

From Dev

unable to pass char[] to generic method

From Dev

Unexpected output for address of char array

From Dev

Array type char[] is not assignable

From Dev

Why is a class member of type char array suddenly of type const char array in member function?

From Dev

Taking part of a char array and putting it into a string

From Dev

Taking part of a char array and putting it into a string

From Dev

Passing char array to function

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

How many ways are there to pass char array to function in C?

From Dev

Pass a char array to a function template such that gcc can tell it is a literal?

From Dev

Pass char pointer to function in C

From Dev

simply accessing char array in a function by char pointer

From Dev

function warnings C: "warning: type of ‘char_array’ defaults to ‘int’"

From Dev

Incompatible types when assigning to type 'char[100][100]' from type 'char *' error in 2d array

From Dev

Passing an array of an array of char to a function

From Dev

Parsing a char array to array with function

From Dev

Passing an array of an array of char to a function

Related Related

  1. 1

    How to pass an array of Swift strings to a C function taking a char ** parameter

  2. 2

    how to pass 2D array type char (string) as function parameter?

  3. 3

    Pass char pointer array to function

  4. 4

    Pass char pointer/array to a function

  5. 5

    Pass a 2D char array to a function in C

  6. 6

    How to pass an array of char's to a function in C

  7. 7

    Can't pass a char array to a function

  8. 8

    How to pass in function a multidimensional char array in C?

  9. 9

    How to pass in function a multidimensional char array in C?

  10. 10

    pass and modify char array[][] in function C

  11. 11

    How to pass char into function?

  12. 12

    Why address is type casted to char *?

  13. 13

    unable to pass char[] to generic method

  14. 14

    Unexpected output for address of char array

  15. 15

    Array type char[] is not assignable

  16. 16

    Why is a class member of type char array suddenly of type const char array in member function?

  17. 17

    Taking part of a char array and putting it into a string

  18. 18

    Taking part of a char array and putting it into a string

  19. 19

    Passing char array to function

  20. 20

    How to pass a float array to the function that has unsigned char pointer as an argument?

  21. 21

    How many ways are there to pass char array to function in C?

  22. 22

    Pass a char array to a function template such that gcc can tell it is a literal?

  23. 23

    Pass char pointer to function in C

  24. 24

    simply accessing char array in a function by char pointer

  25. 25

    function warnings C: "warning: type of ‘char_array’ defaults to ‘int’"

  26. 26

    Incompatible types when assigning to type 'char[100][100]' from type 'char *' error in 2d array

  27. 27

    Passing an array of an array of char to a function

  28. 28

    Parsing a char array to array with function

  29. 29

    Passing an array of an array of char to a function

HotTag

Archive