Reputation: 1045
Given the Source Code from strcpy()
char * strcpy(char *s1, const char *s2)
{
char *s = s1;
while ((*s++ = *s2++) != 0);
return (s1);
}
Why does handing over the second argument work and how does it look in memory since I do not pass a pointer to the function
char dest[100];
strcpy(dest, "HelloWorld");
Upvotes: 2
Views: 670
Reputation: 311146
In C string literals have types of character arrays. From C Standard (6.4.5 String literals)
6 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.
Also arrays with rare exceptions are converted to pointers in expressions. The C Standard, 6.3.2.1 Lvalues, arrays, and function designators
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
Thus in this call
strcpy(dest, "HelloWorld");
the string literal has type char[11]
that is converted to value of type char *
that is equal to the address of the first character of the string literal.
You could also write for example
strcpy(dest, &"HelloWorld"[0]);
Or even :)
strcpy(dest, &0["HelloWorld"]);
or:)
strcpy(dest, &*"HelloWorld");
All three expressions yield the address of the initial element of the string literal and have type char *
.
Take into account that it is implementation-defined (usually controlled by compiler options) whether
"HelloWorld" == "HelloWorld"
is evaluated to true. That is whether the compiler allocates separate extents of memory for identical string literals or will store only one copy of them.
In this expression the addresses of the first characters of the string literals are compared.
If you write
strcmp( "HelloWorld", "HelloWorld" )
then the result will be equal to 0 that is the string literals are equal each other (contain the same sequence of characters)
Upvotes: 3
Reputation: 134396
This works, because,
For dest
, arrays, when passed as function arguments, decay to the address of the first element. That's a pointer.
So, a call like
strcpy(dest, "HelloWorld");
is the same as
strcpy(&dest[0], "HelloWorld");
For "HelloWorld"
, a string literal, has a type of char[]
. So, it essentially gives you the address of the fist element in it.
Upvotes: 8
Reputation: 16607
strcpy(dest, "HelloWorld");
dest
being and array of char
will decay to pointer to base element when passed to function and accessed there .
And "HelloWorld"
being a string literal is of type char []
(but not modifiable) and hence is correct argument to pass.
Upvotes: 1