This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
In VS C++ I have a simple declaration of a char variable and a pointer to char.
char mychar = 'c';
char *mypointer = &mychar;
When printing the value of mypointer using cout I would expect an 8 character address value like 0038FEDC to appear in console. Instead I am getting some strange results like:
c│■8, ck∙<, c;■8 etc...
Why these strange characters appear when outputting pointer to char values?
std::ostream, of which std::cout is an instance, has an overload for operator<< which treats char* as a pointer to the first character in a null-terminated string.
You are passing it a pointer of a single character, not a null terminated string. This causes undefined behaviour.
In practice what is likely to happen is that a stream of characters will be printed out by treating the memory starting from mychar as an array of char and iterating over it, until a \0 is found.
If you want to print the address, you can cast the pointer to something that isn't char*:
std::cout << static_cast<void*>(mypointer) << std::endl;
Related
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 2 years ago.
I am a little confused here. In the statement const char *str="Hello";, str is a pointer to a char variable pointing to the first character 'H' of the string "Hello", so str should contain the address of the 'H' character. And yet, if I use cout<<str, it prints the entire string "Hello" and not the address.
And also, If I use cout<<*str to print the value stored in the address pointed to by str, it prints the char 'H' correctly.
Can someone please explain how and why this happens? This may be very basic, but an explanation would help me understand these concepts more clearly.
str is a pointer to a char variable
There is no char variable in the example. str does point to a char object.
Can someone please explain how and why this happens?
Because the binary operator << whose left hand operand is an output sream and right hand operand is a pointer to char is specified to print the entire string.
If the pointed char isn't within an array that contains the null terminator character starting from that character, then the behaviour of the program would be undefined. String literals are null terminated, so the example is correct.
This question already has answers here:
Does C have a string type? [closed]
(7 answers)
Closed 5 years ago.
Say that in a class student, I had written the following public member function:
char *p()
{ return (name);}
Now, name is actually a datamember(private section) and is a character string. I assume that this means char* can return a string. If I were writing something similar like:
void main()
{ char *s = "GOODLUCK";
cout<<*s;
}
What does *s give me here? I think it's the whole string 's'. Am I correct?
Description below is far from pedantic, but should help you to start digging into pointers:
char *s means, that s is pointer to char. In other words, it is variable, which can store address of area, capable of storing char. BTW I prefer writing char* s (space moved) as more descriptive: s is variable name, char* is variable type.
"GOODLUCK" is an array of 9 chars (the last one is '\0'). All array memebers are always kept one after another in memory, so if you know address of the first member, you can find address of the second, the third and so on. That's why arrays in C++ are handled as address of the first member.
s = "GOODLUCK"; means "store array address (aka address of the first element) into s". So s now points to the first char of "GOODLUCK".
*s means "get data, which variable points at". s points to the first char of string, so *s is simply a G
cout << *s; means "calculate *s and pass it to cout", so it's identical to cout <<'G';
writing cout << s; (note missing asterisk) doesn't pass one char to cout, but passes pointer to char. cout expects pointer to char to be passed only when it represents address of array, not address of single char. cout will print one by one all members of array, so the whole string will be displayed.
Bottom line: char *p() { return (name);} actually returns address of the first char of the name, but address of the first char represents the whole name. Possible further reading
This question already has answers here:
pointers and string literals
(3 answers)
Closed 7 years ago.
I am fairly new to C++,when I was reading about pointers I got confused a bit:
char str[] = "Alex"
char *ptr
ptr = str
cout << ptr
This gives "Alex" string itself , rather than the memory location and also, *ptr would give you the letter "A".Would someone explain to me why this is happening please?
Also, in addition to this.When I tried printing
cout << *ptr
it gives me address of 1st character.why is this ?, i was expecting the 1st value instead
operator<< has an overload that takes an ostream (the type of cout) and a const char*.
That overload treats the const char* argument as a null terminated C-string, and prints characters pointed to by that pointer, until it finds the terminating null character.
Note that passing a char pointer that doesn't point to a null terminated sequence of chars invokes undefined behavior.
For circumventing this behavior when you simply want to output the memory address, cast the pointer to void* first:
cout << static_cast<void*>(ptr); // outputs the address stored in 'ptr'
C style strings are just C-style arrays of char, with all the semantics associated to C-style arrays.
iostream is special-cased to behave differently when given a char pointer; if you want to print the address you should cast it to void* first.
it is happen because in c++ you can overload operators, like this:
ostream& operator<<(ostream& os, const char* p)
{
while (*p) cout<<*p++;
}
and because char array is a common type of strings, it was done for you in the standard language.
Because strings in C/C++ ends with '\0'. The output will run to the next pointer until it reaches the pointer wich point to '\0'.
ptr + 0 == 'A';
ptr + 1 == 'l';
ptr + 2 == 'e';
ptr + 3 == 'x';
ptr + 4 == '\0'; // stop, end of output
This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 8 years ago.
char label[] = "Single";
char *labelPtr;
labelPtr = label;
cout<<labelPtr;;
return 0;
The ouput with the above code is Single.
I'am confused about the output.
As far as my understanding labelPtr should contain the address of label[0]. So shouldn't the output be equivalent to &label[0]?
std::cout treats char* as a pointer to a C-style string.
So the whole C-style string is printed.
Consider casting to (void*) to see what will happen.
labelPtr = label;
In this label refers to the first element of the char array. And then you are making labelptr point to that same array. That means from now onwards you can use labelptr in a same way you use label.
So, both are equivalent:-
cout << label;
cout << labelptr;
As cout considers the argument to be of type char*. All the contents of the string is printed.
For you to print only the first character try using
cout<<*labelPtr;;
This would print only the first character. To print 2nd character use cout<< *(labelPtr+1);;
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
Below code prints the entire string. I am confused why its does so.
char test[] = "jeff";
cout<<test<<endl;
The output is "Jeff", I was expecting it to print the value of char array "test", since test is pointer, pointer the first element which is 'J'.
Why is it printing the whole string, when I cout<<test??
Because of operator<< (basic_ostream<charT,traits>& os, const char* s); (#2 "character sequence" in that list) (slightly more technical list). test decays to a pointer, or char*, which then gets printed as a C-string.
It's the exact same reason cout << "Jeff"; works (instead of printing the address of "Jeff").
The first element is 'j', certainly, but a char* isn't meant to represent only one char, but a string of them. cout will keep reading chars til it find the null char, or '\0'. This is implicitly put there when you use a string literal such as "jeff".
To print only the first char, dereference the pointer to get it like cout<<*test<<endl;.
In C++ (as in C), strings are modeled as NUL-terminated character arrays I.e., the last character's ordinal value is 0, in your example it's character with index 4, inserted by the compiler immediately after the last "f" in "jeff". So in many contexts pointers to "char" are assumed to be NUL-terminated; in this case "cout" keeps printing characters until it hits the NUL character at the end, at which point it stops. C++ also has an actual string class, "std::string", that is in many ways superior to char arrays.