0

I am a student studying C++ and recently studied about pointers. Here is a code written by me and I am having trouble explaining myself the output I get

int main()
{
    char *names[]={"abc", "def", "ghi", "jkl", "mno"};
    cout << (names[0]);
    return 0;
}

Now , here I have made an array of pointers and the first pointer should point at the first element of the first string i.e to a and that it is doing. But when I want to cout the pointer instead of giving the address of a, it is printing abc.

I am not able to find the reason behind this and so it would be really helpful for me if someone can explain me this.

mch
  • 9,424
  • 2
  • 28
  • 42
  • printing the pointer is done by: cout << names ; – Gal Keren May 15 '18 at 13:03
  • If you pass `char *` to `cout` it will treat it as a string. What exactly are you expecting to see? – Killzone Kid May 15 '18 at 13:03
  • what you have is an array of pointers to pointers of type char to get `a` you would do `names[0][0]` in simple terms you have a pointer which contains "other" pointers. names[0] accesses the first inner pointer passing that to cout will print the whole thing. – Samer Tufail May 15 '18 at 13:05
  • You can always take an address with `&`: `std::cout << &names[0]` – Jaa-c May 15 '18 at 13:10
  • Which pointer are you trying to print? The pointer to `names`? The pointer to `names[0]`? The pointer to `names[0][0]`? – Some programmer dude May 15 '18 at 13:18
  • @Someprogrammerdude "instead of giving the address of a, it is printing abc." it is in OP's question already – Slava May 15 '18 at 13:19
  • @Someprogrammerdude OP already getting pointer to a, that not the issue, problem is how `std::ostream` treats `const char *` – Slava May 15 '18 at 13:24
  • Isn't names[0] a pointer itself and hence when I print it, shouldn't it give me the address of the thing it is pointing to? – Abhinav Dhawan May 15 '18 at 13:26
  • 2
    The proper way to get a pointer to something is through the address-of operator `&`. However, getting a pointer to a `char` is not enough, since [there is a standard overloaded `<<` operator](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) for `char *` that treats such pointers as null-terminated byte strings (i.e. C-style strings). If you want to get a pointer to a character, and print the actual pointer, you need to *cast* it: `static_cast(&names[0][0])` (or optionally `static_cast(names[0])`) – Some programmer dude May 15 '18 at 13:26
  • 1
    @AbhinavDhawan yes it is pointer, but `char *` pointers treated special way since C. It is well explained in the duplicate. So just cast it to `void *` and `cout` will give you address – Slava May 15 '18 at 13:28
  • Thanks all of you. I will surely read the posts and would try to understand this. – Abhinav Dhawan May 15 '18 at 13:29

1 Answers1

-2

Try this:

cout<< &names[0];
LawfulGood
  • 177
  • 5
  • The question asks for a reason, and you don't provide one. Explain what's the problem, and why your answer solves it. – Laurenz Albe May 15 '18 at 20:04