0

I have a simple test code for Function Pointer:

void PrintHello(char *name)
{
    printf("Hello %s\n", name);
}
int main(int argc, const char * argv[])
{ 
    //ptr is a function pointer
    void (*ptr)(char*);

    ptr = PrintHello;
    ptr("world");

    return 0;
}

The code build & run successfully. The "Hello world" string is printed out.

But, what I don't understand is, the function PrintHello(char*) accepts a pointer to string as argument. But my code calls this function through Function Pointer ptr("world"), in which I directly passed the string "world" to the function, not a pointer to string. Why it works?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • It's a pointer to `char`, not a pointer to a string – blgt Feb 19 '15 at 11:11
  • 1
    `char*` isn't a ***pointer to string***. _`"hello"`_ is actually a `const char*`. SO might not be the best place to learn the basics of c++ BTW. – πάντα ῥεῖ Feb 19 '15 at 11:12
  • Also note that a string literal becomes a `const char *`, not a `char *`. You are getting away with it because you're compiler is being lenient (this usage used to be commonplace, so compilers may choose to support it even though it is not strictly legal). – BoBTFish Feb 19 '15 at 11:13
  • 2
    Actually I notice this is tagged `C` and `C++`. This is a difference between the two: In C++ `"foo"` has type `const char[4]`, decaying to `const char *`. In `C`, you don't get the `const`s. – BoBTFish Feb 19 '15 at 11:15
  • 2
    @πάνταῥεῖ: No, `"hello"` is an array, not a pointer. And since when were basic questions frowned upon? This is perfectly answerable. – Mike Seymour Feb 19 '15 at 11:19

2 Answers2

4

In C string literals are of type char []. Passing a string literal to a function means that you are passing a pointer to string's first element which is of char * type. The function call

ptr("world"); 

is equivalent to

char name[] = "world"; 
ptr(name);          // Name decays to pointer to first character of the string literal.  

It should be noted that string literals are non modifiable and hence "world" in ptr("world"); but same is not true for char arrays.

In C++ the string literals are of type const char []. It means that function ptr must have a parameter of type const char * instead of char *. (const char [] will decay to const char *).

haccks
  • 104,019
  • 25
  • 176
  • 264
1

the function PrintHello(char*) accepts a pointer to string

No, it doesn't. It accepts a pointer to char.

But my code calls this function through Function Pointer

It doesn't matter WRT arguments' passing at all.

I directly passed the string "world" to the function

You didn't. You passed a literal, which is the same as passing a pointer to the first element (char* in C, const char* in C++).

Why it works?

As mentioned, the fact the function is called via a function pointer doesn't matter. So it works like a regular call. That being said, in C++ it's illegal to pass a string literal to a function taking a non-const pointer.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Hi, I thought in C "pointer to string" is the same as "pointer to char", since string is an array of char. Could you please clarify this? thanks. BTW, I didn't downvote you, it is not me :) – Leem.fin Feb 19 '15 at 11:36
  • @Leem.fin that's what you get for mixing two different languages in one question. That being said, I am pretty sure string literals in C are still string literals. – Bartek Banachewicz Feb 19 '15 at 11:37