Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a reason strcpy's signature is this:

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.

Am I misunderstanding what const pointers should be used for? In my mind, when a function I write accepts a pointer which won't be changed (via realloc, etc.), then I mark it as a const pointer so the caller can be assured their pointer won't be moved on them. (In case they have other structs/etc. referencing that pointer location that would become outdated)

Is that an OK practice, or will it have unintended consequences?

like image 668
Rothwell Avatar asked Jan 23 '17 16:01

Rothwell


People also ask

What is the difference between SRC and DEST in strcpy?

src: The string which will be copied. dest: Pointer to the destination array where the content is to be copied. Return Value: It returns a pointer to the destination string. Problem with strcpy (): The strcpy () function does not specify the size of the destination array, so buffer overrun is often a risk.

Why strcpy and strncpy are not safe?

Why strcpy and strncpy are not safe to use? The strcpy () function is used to copy the source string to destination string. If the buffer size of dest string is more then src string, then copy the src string to dest string with terminating NULL character.

How does strncpy work in C?

The C library function char *strncpy (char *dest, const char *src, size_t n) copies up to n characters from the string pointed to, by src to dest. In a case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.

What is my_strcpy () function in C++?

The my_strcpy() function accepts two arguments of type pointer to char or (char*) and returns a pointer to the first string.


3 Answers

The source code for strcpy is very roughly this:

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

Here we actually do modify dest but this has no consequence for the caller because inside the strcpyfunction, dest is a local variable.

But following code won't 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 a non necessary temp variable.

like image 186
Jabberwocky Avatar answered Oct 16 '22 20:10

Jabberwocky


Qualifiers on function arguments are completely ignored in declarations (prototypes). This is required by the C language, and it makes sense, because there is no possible meaning such qualification could have to callers of the function.

In the case of const char *src, the argument src is not what's qualified; the type it points to is qualified. In your hypothetical declaration for dest, the qualifier applies to dest and is thus meaningless.

like image 3
R.. GitHub STOP HELPING ICE Avatar answered Oct 16 '22 19:10

R.. GitHub STOP HELPING ICE


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

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

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

In 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 calling code cares about is if the data pointed to by s or d is changed and that is control by the const of the left side of *

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`.
like image 2
chux - Reinstate Monica Avatar answered Oct 16 '22 20:10

chux - Reinstate Monica