I am new to C programming. and I know char * and char[] array are different. Yet, you can deduct the char[] to char * when it comes to a function param. So function declarations could be the same.
But how do I know if the function is specifically expecting a char array versus char * by looking at the signature (declaration)?
For example, if I am using a library header file and the function is below. How do I know which one to pass?
// somelib.h
void foo(char *bar);
Because if the function is modifying the bar parameter and if I pass a char *, it will get a segfault. This is for C, but would it be the same for C++?
If the function modifies the array, the argument should be declared char *. If it doesn't modify the array, the argument should be declared const char *.
When the argument is declared const char * it will be safe to pass a string literal. Otherwise you should pass an array or a pointer to memory dynamically allocated with malloc() (in C) or new (in C++).
In C++ you'll get a compilation error if you try to pass a literal to a parameter without the const modifier, because string literals are const. But for historical reasons, the type of string literals in C is non-const, even though modifying them results in undefined behavior, so there's no error (but some compilers will produce warnings).
as function param
I know
char *andchar[]array are different
Although char * and char[] are different types, they are not different as function a function parameter, because all function parameters declared as an array are adjusted to be pointers to element of such array. As such, char[] function parameter is adjusted by the compiler to be char *. This adjustment applies only to function parameters and not to other contexts.
But how do I know if the function is specifically expecting a char array versus char * by looking at the signature (declaration)?
A function that expects a char array is a function that expects a char *. I suppose that you mean to ask how to know whether the function expects/requires that the char * will be pointing to an element of an array. And also, perhaps whether the array needs to be null terminated, or have a minimum size.
The answer is that you cannot know it by looking at the signature alone.
how do I know which one to pass?
By reading the documentation that describes what the function does. If there is no documentation, then read the implementation of the function. If there is no implementation either, then you could attempt reverse-engineering the binary. If you don't know what the function does, then you shouldn't call it.
because if the function is modifying the bar parameter and if I pass a char *, it will get a segfault.
I suppose that you mean that the function modifies the pointed char or chars. Modifying the pointer itself should be fine in this case.
If the array whose element is pointed by char * is modifiable, then passing it into a function that modifies the array shouldn't cause a segfault. If the array is const, then you shouldn't pass it into a function that modifies the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With