Search code examples
cfunctionarguments

What is the difference in using variable as function argument and in using directly string as function argument?


I was making some function that takes string as function argument but

// This is working fine
char string[] = "Any string";
func(string);

//This is not working
func("Any string");

Please tell me the difference


Solution

  • As per your description, it seems that the prototype of your function

    func(char* str);
    

    OR

    func(char str[]);
    

    Now the problem comes in the datatype of the String. When you call func("Any string") which is expecting a pointer value (as an Array also works as a pointer) you are giving it a string value which will generate an error as the arguments are miss-matched.

    If you still want to pass a string use this:

    str = "Any String";
    func(str.toCharArray());