Pass a char array to a function that takes a char pointer


Bruno Ely

In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **?

UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change the compiler warning

E.g:

#include <stdio.h>

void foo(char* qux) { puts(qux); }
void bar(char** qux) { puts(*qux); }
void baz(char* qux[12]) { puts(*qux); }

int main() {
    char str[12] = "Hello there";

    foo(str);
    bar(&str); // Compiler warning
    baz(&str); // Same compiler warning

    return 0;
}

In the second case I get a compiler warning:

warning: incompatible pointer types passing 'char (*)[12]' to
         parameter of type 'char **' [-Wincompatible-pointer-types]

What's going on here?

some programmer buddies

Arrays decay naturally to a pointer to the first element. So in a call, foo(str)it's effectively the same as foo(&str[0]). It's type, char *so it's ok.

Now, the second call is a barproblem. When used &str, instead of getting a pointer to the first element of the array, you get a pointer to the array itself . And as the compiler points out, this is the type , which is very different from (and is not compatible with) .char (*)[12]char **

Finally, when declaring, bazyou say the parameter is type char *[12], i.e. you have an array or 12 pointers to char , not that you have an array of 12 pointerschar . Also, since the array decays to the pointer stuff, it 's char *[12]actually the same .char **

Related


Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass pointer to char array to function

bob jane Below is my function that takes a char array of size 2, passes it to the function, and the function should return the same 2 chars (it's a bit complicated when it comes to communicating with hardware devices). The problem is when I pass char(*in)[2] t

Pass char pointer/array to function

open I'm trying to understand more about char pointers in C, but one thing got me. Suppose I want to pass a char pointer to a function and change the value represented by that pointer. Here is an example: int Foo (char *(&Msg1), char* Msg2, char* Msg3){ ch

Pass char pointer/array to function

open I'm trying to understand more about char pointers in C, but one thing got me. Suppose I want to pass a char pointer to a function and change the value represented by that pointer. Here is an example: int Foo (char *(&Msg1), char* Msg2, char* Msg3){ ch

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass char array to function

under the moonlight I have to call a method with the following signature: int sendTo(const void* buffer, int length, const SocketAddress& address, int flags=0); My first question is: What exactly const void* bufferdoes that mean? My intention: it means it's a

Pass char array to function

under the moonlight I have to call a method with the following signature: int sendTo(const void* buffer, int length, const SocketAddress& address, int flags=0); My first question is: What exactly const void* bufferdoes that mean? My intention: it means it's a