char* is not an array, it's a pointer. It's best not to think of 'char*' as a string, but instead, think of it as a pointer to a string.
C-style strings are not single objects, but are a series of variables (one for each character), each one must be copied individually. There are functons which automate this process so it can be done in one step:
Typically you do not return pointers or arrays from functions for this kind of thing (for several reasons). Instead, you pass a pointer to a buffer you want to fill and have the function fill that pointer. For example to do what you are trying to do above, you could do this:
My understanding is that * will deference the pointer and will give a value and without * will give the address. But here p="bye" works and able to display it also ....can some one clear my doubt ?
To make things a little simpler, let's suppose you're not calling std::cout.operator<<(). Let's suppose you're calling a function printString().
1 2 3 4 5 6
void printString(constchar *str){
for (;*str;str++)
printf(*str);
}
//and it's called like this:
printString(p);
This is what's happenning:
1. When you pass p, which is a pointer (pointers are memory addresses) to a character string, you're passing a copy of a memory address.
2. The function receives a memory address. It doesn't actually care what it points to; it will execute whether it points to a valid string or to something completely different.
3. Simplified the function's loop looks like this: while (the char value at the memory address 'str' is not zero){
print the char value at the memory address 'str';
increment the value of 'str' by 1;
}
The result is that the C string the original address pointed to (or whatever there was at that memory location) is printed correctly.