1

I have the following C++ code-

#include<iostream>

using namespace std;

int main(){

char a[]={'0','1','2','3','4','5','6'};
char* p=a;

cout<<p;

return  0;
}


The pointer p here contains the address of char array a.
The output is -

01234565ÿ"


I had used a integer array which gives me a simple hexadecimal address as output.
Please explain me this strange output for char array.

Shivam Aggarwal
  • 734
  • 2
  • 9
  • 23
  • 3
    You're not printing the address of `a`, you're printing `a` as if it was a C, NUL-terminated string, which is not. – YSC Jan 05 '16 at 16:31
  • 1
    @Brut3Forc3 In `C`, and inherited by `C++`, an array of `char` (or pointer to `char`) is treated as a textual string in a lot of contexts. It has to be terminated with `0`, or "NUL" character, denoted as a `char` by `'\0'`. Which in this case, it isn't, which is why you see some garbage at the end, it just keeps printing until it happens to hit a byte containing `0`. – BoBTFish Jan 05 '16 at 16:33
  • @Brut3Forc3 done, see answer ;) – YSC Jan 05 '16 at 16:37

4 Answers4

3

You are printing it as a string. So it will print all chars from start of a and until it see a 0x0 which is the end of string

If you add a termination to the array, like:

char a[]={'0','1','2','3','4','5','6', 0x0};
char* p=a;

cout<<p;

then the output will be:

0123456

If you want to print the address of a (instead of the values in the array) you need to cast the char* to a void* first

cout << static_cast<void*>(p);
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
2

Cast a as a void* to avoid std::iostream::operator<<(const char*) being called:

#include<iostream>

int main()
{
    char a[]={'0','1','2','3','4','5','6'};
    std::cout << static_cast<void*>(a) << std::endl;
}

Please explain me this strange output for char array.

std::iostream::operator<<() exist for various argument types, each one is handled differently:

  • std::iostream::operator<<(const char*) prints a C-style string, this is what is called when you write std::cout << p.
  • std::iostream::operator<<(const void*) prints a pointer as an hexadecimal integer, this is what is called when you write std::cout << static_cast<void*>(a).

I had used a integer array which gives me a simple hexadecimal address as output.

If you'd declare an array of integers, printing it wouldn't call the const char* version but the const int* version if it existed. Since it's not defined, the compiler defaults to the const void* since a pointer can be implicitly cast to a const void*:

#include<iostream>

int main()
{
    int a[]={'0','1','2','3','4','5','6'};
    const int* p = a;
    std::cout << p << std::endl; // prints address of a.
}
YSC
  • 38,212
  • 9
  • 96
  • 149
2

The output stream operator interprets an argument of (const) char* as a NUL-terminated string. Since your character sequence is not NUL-terminated (e.g. with '\0'), you get garbage after 123456.

Any other pointer type will indeed be interpreted as a pointer and output as such. So if you cast p into another type, e.g.

cout << static_cast<const void*>(p);

you will get the expected result.

mindriot
  • 5,413
  • 1
  • 25
  • 34
2

cout has an overload that takes a const char * and prints its contents. Since a char* can be passed to a function taking a const char * this is the function that is called. It will keep printing from the start of the array pointed to by the pointer until it sees an null(0) element. This is why you get the extra output as your array is not null terminated. If you want print the address you would need to use

cout << static_cast<const void*>(p);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402