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?

5
  • It's a pointer to char, not a pointer to a string Commented Feb 19, 2015 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. Commented Feb 19, 2015 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). Commented Feb 19, 2015 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 consts. Commented Feb 19, 2015 at 11:15
  • 2
    @πάνταῥεῖ: No, "hello" is an array, not a pointer. And since when were basic questions frowned upon? This is perfectly answerable. Commented Feb 19, 2015 at 11:19

2 Answers 2

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 *).

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

1 Comment

C++'s std::string has an automatic conversion from "(const) char*" to std::string.
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.

2 Comments

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

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.