How to know where the pointer is pointing in char *?

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()
{
  int temp1={1,2,3};
  int *temp2=temp1;
  cout<<temp2;
}


The above code will print the location of the place where temp2 is pointing...

Now in the folllwoing code:

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main()
{
  char *temp1="Hello";
  //cout<<________ ; what should i write to know the location of 'H'
}
std::cout << (const void *)"Hello";
Last edited on
closed account (EhqpDjzh)
Try Helios's answer first, but in a string, the location of the first character is also the location of the string. In this case, H is the first character. A more simple solution could be:
cout << temp1;
std::cout has a overload for char* that prints out the C-string, so your method won't work like you expect. Hence why you must cast it to something like a void* before trying to print it.
Topic archived. No new replies allowed.