This question already has answers here:
C++ return string keeps getting junk
(1 answer)
ostream cout and char *
(3 answers)
Closed 8 years ago.
I'm new in c++. I wrote this program.
#include <iostream>
using namespace std;
int main(void) {
int x=24;
char y='A';
char* pchar=&y;
int* pint=&x;
cout <<"pchar= "<<hex<<&pchar<<endl;
cout <<"pint = "<<hex<<&pint<<endl;
cout <<endl;
cout <<"pchar= "<<hex<<pchar<<endl;
cout <<"pint = "<<hex<<pint<<endl;
pchar=NULL;
pint=NULL;
getchar();
return 0;
}
and its result
Can you help me to understand why I can't print the address of variables without &?
I think pchar is already the address of y.
thanks
When you use a char* (like your variable pchar) the string overload of operator<< function is used, and when treating it as a string it will print characters until it find the string terminator character ('\0'). Unfortunately in this case pchar points only to a single character, so you have undefined behavior as the function goes out looking for characters in memory not allocated to you.
To print a pointer you have to cast it to void*:
std::cout << "pchar = " << std::hex << reinterpret_cast<void*>(pchar) << '\n';
It works when you print the address of the variable pchar (when you print &pchar) because then the type of the expression is of type char** which have no direct overload, and instead uses the generic pointer overload (case 7 in this reference).
Operator << treated char* as a string. But you can change your code to following
cout <<"pchar= "<<hex<<(void*)pchar<<endl;
It should work.
I cannot access std code now, but cout implementation should contain something like this
operator << (char* char_ptr)
{
//interpret char * as pointer to null-terminated string
... code to output string
}
So there is impossible to print char* as address without casting to something.
You'll need
cout <<"pchar= "<<hex<<static_cast<void*>(pchar)<<endl;
to print the value of pchar (not its address, that would be the address of the variable pchar, not the address to which it points to).
It behaves like this because std::ostream (cout's type) has an overloaded operator<< which takes a char* and treats it specifically to print out a string.
Change this statement
cout <<"pchar= "<<hex<<pchar<<endl;
to
cout <<"pchar= "<<hex<< ( const void * )pchar<<endl;
The problem is that when you write this way
cout <<"pchar= "<<hex<<pchar<<endl;
the compiler considers pchar as an address of the first element of a string and tries to output this string. There is an overloaded operator << for type char * and it is called in this case.
pchar value is address of y. So if you print pchar it will print address of y. &pchar, it will print address of pchar. Remember pchar also have some address.
Related
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 2 years ago.
I have written a simple C++ code, and its working fine. But I don't know how it is working. I am just replacing "l" with "r" using myfun().
The return type of myfun() is char*. If I am returning &(str[0]), that is, only the address of the first element of the array, then why is it printing the complete string "herloworld"? Please explain what return &(str[0]); is doing.
#include <iostream>
using namespace std;
char* myfun(char str[])
{
str[2] = 'r';
return &(str[0]);
}
int main()
{
char str[] = "helloworld";
char* word;
word = myfun(str);
cout << word;
}
The operator << is overloaded for the type char * such a way that it expects that the used pointer of the type char * points to the first character of a string.
So the operator outputs all characters of the passed string until the zero character '\0' is encountered.
Also pay attention to that arrays used in expressions with rare exceptions are converted to pointers to their first elements.
So this call
word = myfun(str);
is equivalent to
word = myfun( &str[0]);
On the other hand, a function parameter having an array type is implicitly adjusted to pointer to the element type.
So this function declaration
char* myfun(char str[]);
is equivalent to the following declaration
char* myfun(char *str);
The both declarations declare the same one function.
And within the function instead of this return statement
return &(str[0]);
you could write
return str;
Correspondingly in main you could write
cout << myfun(str);
without declaring and using the intermediate pointer word.
If cstring is a pointer, then why can it get a value directly? Secondly, Why wasn't the *cstring's result equal to whole of string? Third, cstring is a non-constant pointer to a constant character, so why change its value and not change its address?
#include <cstdio>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
const char* cstring = "string";
cout << cstring << endl << *cstring << endl << &cstring << endl;
cstring = "foo";
cout << cstring << endl << *cstring << endl << &cstring << endl;
_getch();
return 0;
}
If cstring is a pointer, then why can it get a value directly?
The operator << of cout for const char* is specialized to behave that way. It will treat the pointer as a NULL terminated string and will print it instead of the pointer value. For different types you get different behaviour. For char* you have the whole string printed.
Why wasn't the *cstring's result equal to whole of string?
That is because the type of *cstring is char and again, operator << behaves correctly by just printing a single char. a const char* is essentially an array of char.An array is essentially a pointer to the first element of the array. If you use the * operator on a pointer you are accessing whatever the pointer points to. If it points to the first element, well, you get the first element.
Third, cstring is a non-constant pointer to a constant character, so why change its value and not change its address?
As you said, cstring is a non-constant pointer to constant data. You cannot change the place it points to (it is a constant pointer), but you can substitute the content of the pointed data with other stuff. You point to the same location but the content of that cell changes.
If cstring is a pointer, then why can it get a value directly?
Anyone can get the value pointed at by a pointer by dereferencing the pointer. That's what happens when you do std::cout << cstring. The proper overload gets chosen that prints the string represented by cstring assuming that is correctly formed, null-terminated C-style string.
Secondly, Why wasn't the *cstring's result equal to whole of
string?
cstring is a const char*, so *cstring is a const char. Pass that to std::cout and it will call an overload that prints one char. The function that is called internally doesn't even know that this is just one char in a string.
Third, cstring is a non-constant pointer to a constant character, so
why change its value and not change its address?
You can't change the address of a variable. cstring is in a fixed place on the stack. You change the value of cstring, which is the address of the string that it's pointing to (it is now pointing to a different string, which has a different address, "string" of course stil has the same address).
What you probably wanted to try is this:
const char* cstring = "string";
std::cout << (void*)cstring << std::endl;
cstring = "foo";
std::cout << (void*)cstring << std::endl;
Now you can see the different addresses. One is the address of "string" and one is the address of "foo".
This question already has answers here:
Printing C++ int pointer vs char pointer
(3 answers)
Closed 3 years ago.
For instance,
const char str[] ={'H','e','l','l','o', '\0'};
const int nums[] = {1,2,3,4,5};
cout << str << " " << nums;
gives:
Hello 0x7ffff85c0cf5
Obviously, if I wanted to get addresses of both, I could do
cout << &str << " " << nums
But I'm wondering why there's a difference between this array of integers and an array of characters (string literal). Is this a special case preserved only for strings?
Yes you're absolutely correct. The << operator on ostream has a special overload for a const char*.
There's no such overload provided for an int* - in that case the const void* overload is picked.
There is an overloaded operator<< for const char*, which your const char[] array decays to. This overload treats the char data as a null-terminated string, printing out characters until a null character is reached. However, your char[] array does not have a null terminator in it. You are lucky you are seeing only Hello and not extra garbage, or crashing the app. Your code has undefined behavior because of this.
There is no overload of operator<< for a const int[] array. Your array decays to const int*, which is handled by the operator<< overload that takes a const void* as input, which prints out the address being pointed at.
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;
I understand from here that the name of an array is the address of the first element in the array, so this makes sense to me:
int nbrs[] = {1,2};
cout << nbrs << endl; // Outputs: 0x28ac60
However, why is the entire C-string returned here and not the address of ltrs?
char ltrs[] = "foo";
cout << ltrs << endl; // Outputs: foo
Because iostreams have an overload for char * that prints out what the pointer refers to, up to the first byte that contains a \0.
If you want to print out the address, cast to void * first.
cout has operator<<() overloaded for char* arrays so that it outputs every element of the array until it reaches a null character rather than outputting the address of the pointer
cout, and generally, C++ streams, can handle C strings in a special way. cout operators <<, >> are overloaded to handle a number of different things, and this is one of them.