Why doesn't strcpy use a const pointer for dest?

Is there a reason this is happening:

char *strcpy(char *dest, const char *src); 

instead of this?

 char *strcpy(char *const dest, const char *src); 

As far as I know, the function will never change the pointer.

I do not understand for what constant pointers should be used? In my opinion, when the function I'm writing accepts a pointer that will not be changed (via realloc, etc.), then I mark it as a pointer to const, so that the caller can be sure that their pointer will not be moved to them. (If they have other structures / etc., Referencing the place of the pointer that is becoming obsolete)

Is this a common practice or will it have unintended consequences?

+6
source share
5 answers

The source code for strcpy something like this:

 char *strcpy(char *dest, const char *src) { while (*dest++ = *src++); } 

Here we are really modifying dest , but that doesn't matter to the caller, because inside strcpy , the dest function is a local variable.

But the following code will not compile because dest is const:

 char *strcpy(char * const dest, const char *src) { while (*dest++ = *src++); } 

we would need to write this:

 char *strcpy(char * const dest, const char *src) { char *temp = dest; while (*temp++ = *src++); } 

which introduces the optional temp variable.

+11
source

Qualifiers of function arguments are completely ignored in declarations (prototypes). This is required for the C language, and it makes sense because there is no possible value that such a qualification can have for calling functions.

In the case of const char *src argument is not qualified; the type he points to is qualified. In your hypothetical declaration for dest qualifier refers to dest and therefore does not make sense.

+3
source

char *foo(char *const dest, const char *src) { ... } means that the dest pointer will not change in the body of the function.

This does not mean that the data pointed to by dest will or will not change.

const char *src assures the invocation code that the data pointed to by src will not change.

When calling a function like strcpy(d,s) or foo(d,s) , the calling code does not care if the function changes its copy of the pointer. All the code that invokes the code is related to the fact that the data pointed to by s or d is changed, and this is control using the const left side *

 char *dest, // data pointed by `dest` may or may not change. const char *src // data pointed by `src` will not change change because of `src`. 
+2
source

It makes no sense to mark it as const, as you suggest. In C, function arguments are passed as a copy. This means that the dest variable inside strcpy() actually a new variable (pushed onto the stack) that contains the same content (here, the address).

Take a look at this function prototype:

 void foo(int const a); 

This has no semantic meaning, because we know that the original variable a, which we passed to foo() , cannot be changed, because it is a copy. Only a copy will potentially change. When returning foo, we guarantee that the original variable a does not change.

In the parameters of the function, you want to use the const keyword only if the function can really constantly change the state of the variable. For instance:

 size_t strlen(const char *s); 

This means that the contents of the variable s (i.e. the value stored at address s points to) as const. Therefore, it is guaranteed that your string will be immutable when the string returns.

+1
source

As far as I know, the function will never change the pointer.

Yes, but you can. You can change the pointer. No need to specify a dest const pointer.

For instance:

 int main(void) { char *s = "Hello"; char *d = malloc(6); strcpy(d, s); puts(d); strcpy(d, "World"); puts(d); } 
-one
source

Source: https://habr.com/ru/post/1014337/


All Articles