Reputation: 1099
What is the use of declaring arguments of a function in C as const? Does it have an impact on performance?
e.g. why would you prefer f1 to f2? Or f2 to f1?
void f1(const char* arg) {
/* some code */
}
void f2(char* arg) {
/* some code */
}
Upvotes: 1
Views: 1900
Reputation: 12732
You prefer
void f1(const char* arg)
If you don't want to modify the object pointed by arg through arg. But you can modify the arg itself.
You prefer
void f2(char* arg)
If you want to modify the object pointed by arg through arg.
Upvotes: 1
Reputation: 3774
const char* arg
means that arg
is a pointer to a constant char
. If you try to change the data that arg
points to, you will get an error saying something like: error: assignment of read-only location ‘*arg’
. However, you can change the address contained in arg
.
Whereas, by dropping the const
keyword, you state that the data which arg
points to can be changed.
Your preference should be based on your needs. Have a look at this for more information.
In response to your comment: char * const arg
means means that arg
is a constant (pause a second) pointer to a char
. So, you can't change the address contained in arg
and you can change the data residing at that address.
Upvotes: 2
Reputation: 144695
in void f1(const char *arg)
the argument arg
itself is not const
qualified, it is defined as a pointer to an array of char
that the function should not modify. This is very useful for the reader to understand at first glance that the function f1
does not modify the string it receives.
For example strcpy
is declared in <string.h>
as:
char *strcpy(char *dest, const char *src);
The array pointed to by the first argument is modified, it is the destination array, the second argument points to the source array, which is not modified.
This type of const
qualification is viral: once you define an argument as pointing to a const
object, you can only pass it to functions that have similarly const
qualified arguments.
Regarding the impact on performance, there is no downside. Some compilers might even benefit from this qualifications and generate better code.
So in your example, if f1
does not modify the array pointed to by arg
, unless you need to store f1
in a function pointer of type void (*)(char*)
and cannot change this type, I strongly advise to use:
void f1(const char *arg);
Upvotes: 3
Reputation: 67476
void foo(const char *arg)
declares the pointer to the const char. You cant change the referenced object, but you can change the pointer
void foo(char * const arg)
declares the const pointer to the char. You cant change the the pointer, but you can change the referenced object
void foo(const char * const arg)
declares the const pointer to the const char. You cant change the the pointer and the referenced object
declaring parameters allows many compile time optimizations and may have a significant impact on the performance of the generated code.
It is considered a very good practice to use const
for the parameters and variables which should not change. Is is called the "const correctness"
Futher reading: restrict
keyword.
Upvotes: 2