1

I am studying a course of OOP but I'm new to C++. My instructor introduced us to character array in C++. He said that to get base address of a character array in C++ one can use either of the following:

char* a = "Test String";
cout << &a; // Prints base address of char array

or

char* a = "Test String";
cout << (int*)a;

but when I tried both for same char array I got different results. What is difference between both?

Quentin
  • 62,093
  • 7
  • 131
  • 191
lol
  • 506
  • 1
  • 5
  • 14
  • Better avoid `(int*)` in C++, but first show us a case where it differs! – Wolf Nov 04 '16 at 14:26
  • Neither of those prints the base address (assuming by that the address of the first element of the array is meant) – UnholySheep Nov 04 '16 at 14:32
  • @UnholySheep the second one does, doesn't it ? – Quentin Nov 04 '16 at 14:33
  • In your question title, you promised to ask about arrays, but there is no array question, it's about addresses and pointers. – Wolf Nov 04 '16 at 14:33
  • @UnholySheep So what does that actually prints? – lol Nov 04 '16 at 14:33
  • @Wolf `"Test String"` is an array. – Quentin Nov 04 '16 at 14:34
  • @Quentin but only internally, not on the code level. It's a range of const data. – Wolf Nov 04 '16 at 14:36
  • @Quentin Oh right, my bad – UnholySheep Nov 04 '16 at 14:36
  • 1
    @Wolf I'm not sure what you mean by "internally". A string literal is a constant `char` array, `char[12]` in this case. – Quentin Nov 04 '16 at 14:37
  • @Quentin Well, maybe a const array, but the question does not address this. I typically try to avoid constructions like `&"hello"` and so on, so I treat string literals as *"internal"* entities, like 0 values in initializations (for example in `int i=0;` ) – Wolf Nov 04 '16 at 14:43
  • An instructor available [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) might provide better examples for you to learn about pointers and references. Pointers are something that's well worth getting right since when you get to memory allocation, getting it wrong can be expensive. – UKMonkey Nov 04 '16 at 15:08
  • By the way, nothing in this entire question+answers has anything to do with OOP. – Christian Hackl Nov 04 '16 at 15:12
  • @ChristianHackl Yes this is true that's the reason why OOP isn't one of the tag. I mentioned it so that any person who reads the question can get some background on what I am studying. – lol Nov 04 '16 at 15:17

2 Answers2

12

Change the instructor. This is nonsense. The first snippet takes an address of the pointer (not the character array). The second snippet converts a char pointer to a pointer to int and prints the converted pointer.

The possible reason to do this is that if you try to print a pointer to char, compiler will choose a special overload of operator << and print the string instead of a pointer. However, I'd rather convert it to void* than to int*. The reason is that converting to void* can be done with a safer static_cast.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • 2
    I would add that converting to `void*` is safer, as you would not dereference it by mistake. And it should be `const char *` and `const void *` actually. – Slava Nov 04 '16 at 14:34
  • I couldn't understand your first line "The first snippet takes an address of the pointer (not the character array)." What do you mean by saying that it is not of the character array? – lol Nov 04 '16 at 15:00
  • `char*` = pointer to character (or character array) `char**` = pointer to pointer to character. `&a` = char**; which is not the same as the (void*)a – UKMonkey Nov 04 '16 at 15:05
  • Oh, now I got it &a refers to address of the pointer that represents the character array. Thanks – lol Nov 04 '16 at 15:07
4

If you did not make a mistake, your instructor provided bad example to you. Actual code should be:

char a[] = "Test String";
cout << &a; // Prints base address of char array

vs

char a[] = "Test String";
cout << static_cast<void *>( a ); // Prints base address of first element of char array

Note, assigning string literal to char * is deprecated in C++, it should be const char *

Slava
  • 43,454
  • 1
  • 47
  • 90