(Disclaimer: this answer only covers C++. It is not recommended to ask for both C and C++, as both languages are quite different)
Prologue
char *k ;
This declares a pointer to a character.
"ddddd"
This expression has type char const[6]
, that is, an array of six constant characters. Expressions of this type can be implicitly converted to a pointer to the first element of the array. The type of that pointer is char const*
, that is, a pointer to a constant character. A char const*
is not convertible to a char*
, because char*
permits modification of the character it points to, while char const*
doesn't.
char *d="ddddd";
Due to the rules outlined above, this statement is not correct. You should turn on the warnings on your compiler and it will warn you against this (ideally it would actually be an error, not just a warning). It should be char const* d = "ddddd";
if you really want a pointer or char d[] = "ddddd";
if you want an array. The second form doesn't need const
because it makes a copy, and thus runs no risk of changing the original data, which is const
.
*d
This expression involves the implicit conversion to char const*
mentioned above, and then the indirection operator (the *
) is applied to it, resulting in an expression of type char const&
, that is, a reference to a constant character.
*k
Similar to *d
, this performs indirection on a pointer. In this case, k
is of type char*
, so the resulting expression has type char&
.
*k=*d; //expecting to copy "ddddd" to *k
The assignment assigns a single character, because it's assignment from a reference to a constant character into a reference to a character. It ends up assigning the first character of the string to the character pointed by k
, which is... wait.
Where does k
point to? It was never initialized! This is bound to end in tragedy.
Chapter 1
So, how do we get k
to point somewhere? And how do we copy the whole string from d
into that space?
To copy the six characters of the d
array into k
, we need space for six characters. The simplest way of doing this is to just make an array of six characters.
char k[6];
Now, how to copy the six elements? We can use the C library function strcpy
, which copies null-terminated strings. This function needs a lot of care to use because:
It requires the source to have a null ('\0'
) character marking the end; if the source does not have such a character, the behaviour is undefined and anything can happen. This is why the string "ddddd"
has six characters and not 5: there's an implicit \0
character at the end that the compiler inserts for you.
It requires the destination to have enough space for the whole source string including the null terminator. If the destination doesn't have enough space, the behaviour is also undefined and anything can happen.
This is how this function could be used to make a copy of the string:
std::strcpy(k, d);
Failure to meet either point #1 or #2 can result in anything, from the program seemingly working (if you're unlucky), to acting randomly in strange way, to simply crashing (if you're lucky).
Epilogue
Man, that was a lot of information. Do C++ programmers have to care about these matters all the time? That must be tiresome.
Well, C++ programmers can use std::string
instead of character arrays and get a lot less hassle with this: we can make copies with the assignment operator, we don't need to track the correct sizes, we don't need to care for having the null terminators in place, we don't need to limit the size at compile time, and a bunch of other advantages.
std::string k;
void ffill()
{
std::string d="ddddd";
k = d;
cout<<k<<"\n";
d[1]=5;
cout<<k<<"\n";
}