char array and pointers II

Still seem to be a difference in the way 'char array' and 'int array' are treated. When i execute the program below, printing 'q' will give the address of element 0 of array numbers[0xbf924e4c] as expected. But printing 'p' gives the whole string "Hello"; no address at all.

Both code for int and char are equal, but giving a different result:
- int gives the address
- char gives the stringvalue of the char array

What is this about? And what is the address of letters[]?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  // int and character pointers
#include <iostream>
using namespace std;
int main ()
{
  char letters[] = "Hello";
  int numbers[] = {1,2,3,4,5};
  char * p;
  int * q;

  p = letters;
  q = numbers;

  cout << "p is letters; q is numbers\n";
  cout << "p: " << p << endl;
  cout << "q: " << q << endl;
  cout << "*p: " << *p << endl;
  cout << "*q: " << *q << endl;

  return 0;
}
Often char pointers/char arrays are used to handle strings (so called C strings) so the operator<< has been overloaded to handle char* differently. This is what makes cout<<"hello" output "hello" and not just some memory address where the string array "hello" happen to be located in memory.

To print the address you can cast the char* into a void*.
 
cout << "p: " << static_cast<void*>(p) << endl;
Last edited on

You will have to write &p . If you have c++11 , type that :


1
2
3
  
  auto a = letters;
  auto b = &letters;


then move your cursor to a , then b . It will show you something different.
like so :

1
2
3
 
 a -> char *
 b -> char (b)[6]


The reason is that the operator << deduces it as a string . First of all , because of " "

Here is the code you can output to see :

char letters[] = "Hello";
int numbers[] = {1,2,3,4,5};

auto a = letters;
auto b = &letters;

1
2
3
4
5
6
7
8
  
  cout << "p is letters; q is numbers\n";
  cout << "p: " << (char **)letters << endl; //address
  cout << "p: " << &letters << endl; //adress
  cout << "p: " << letters << endl; //string
  cout << "q: " << numbers << endl; // address
  cout << "*p: " << *letters << endl;  // value of first element same as letters[0]
  cout << "*q: " << *numbers << endl; // value of first element same as numbers[0] 


Reply if you have any comment about that
Okay static_cast works fine. Thank you for that.
Still, if i copy p (the pointer) to a string, let's say: string getp = p; The string getp doesn't get the address, but will again give the whole string value. As if the value of p intrinsik is NOT the address. Or, does iostream has a catch when copying? I find this very anoying. Maybe you could shed some light on this .
string getp = p; will call the string constructor that takes a const char* as argument. The constructor reads the string content by incrementing the pointer until it finds a null character '\0' that marks the end of the string.

Just to demonstrate how it works, this is how you can loop through each character in letters using char*.
1
2
3
4
for (char* p = letters; *p != '\0'; ++p)
{
	cout << *p;
}
Last edited on
This gives me the creeps:
- A char array character and a pointer pntr are defined
- pntr = character;
- to get the address of character i can't use pntr as it gives the value of the string
- I can fall back on &character - but shouldn't it be the same as pntr?
Weird
The pointer points to the first element in the array.

 
char* p = letters;

is the same as

 
char* p = &letters[0];


I know it's a bit weird that arrays can be implicit converted to a pointer to the first element just like that but that's just the way it is.

&letters has the same address as p but there is one big difference. The type is different! The type of p is char* (pointer to char) while &letters has type char(*)[6] (pointer to array of 6 characters).
it should not , char is a special primitive type . For non-primitive type you will get your address.
Did you try with std::printf () ?
For me it does not explain why there HAS TO BE different output for int versus char:
p = letters: Hello
p = &letters[0]: Hello
*p value at: H
q = numbers: 0xbff2525c
q = &numbers[0]: 1
*q value at: 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// arrays and pointers
#include <iostream>
using namespace std;
int main ()
{
  char letters[] = "Hello";
  char * p;
  p = letters;
  cout << "p = letters: " << p << endl;
  p = &letters[0];
  cout << "p = &letters[0]: " << p << endl;
  cout << "*p value at: " << *p << endl;

  int numbers[] = {1,2,3,4,5};
  int * q;
  q = numbers;
  cout << "q = numbers: " << q << endl;
  q = &numbers[0];
  cout << "q = &numbers[0]: " << *q << endl;
  cout << "*q value at: " << *q << endl;

  return 0;
}
Last edited on
Here is a thread to help you understand , but mostly << is a method that a programmer has written to print a string and not an address , you would need to make a static_cast for char * .

http://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed
Topic archived. No new replies allowed.