Array of char vs. array of ints

Hello,

working on arrays, when I noticed that an array of ints didn't behave the same way as an array of chars. What is happening here? I was expecting the same result when printing the tab of chars as I get when printing the table of ints (i.e. memory address to first element)
1
2
3
4
char tab[] = {'a', 'b', 'c', 'd'};
 cout << tab; //prints abcd
int tab2[] = {2, 3};
 cout << tab2; //prints the memory address of the first element. 
Last edited on
An array of char is special.
Because 99.9% of the time, you want to print the string rather than it's address.

It's also necessary for you to ensure that there is a \0 in your array of chars to make it a proper string.

As @salem c said, you are a tad lucky with your char array: the stream would have been expecting a null-terminated string ("c-string") and your code relies on the next memory address containing a null character: it might have contained anything.

Try
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   char tab[] = {'a', 'b', 'c', 'd'};
   for ( auto e : tab ) cout << e;
   cout << '\n';

   int tab2[] = { 2, 3 };
   for ( auto e : tab2 ) cout << e;
   cout << '\n';
}
It's unfortunate, but an array of characters with a null character '\0' at the end is how strings were handled in C, and it's still used a lot in C++ even though it has got the std::string class, so for convenience the << operator has been overloaded for char* to instead treat it as a string.
Last edited on
Thanks!

is the code below the same btw? I was wondering if the 0 was filled in automatically in tab_2.

1
2
char tab[] = {'a', 'b', 'c', 'd','\0'};
char tab_2[5] = {'a','b','c','d'}
Last edited on
Yes they are the same, but only because you have specified the array size to be one larger than the number of characters in the initializer list.

Another way to accomplish the same thing is to use a string literal:

 
char tab_3[] = "abcd";
Last edited on
Thanks!

how do you access the memory address of each char, given that you cannot use "normal" array rules?
There is no difference between an array of characters compared to any other array from the language point of view. The difference is how the standard library (in this case the << operator) handles it.

So use it like normal, except when you want to print the address of one of its elements, then you can cast it to void* before printing.

 
cout << static_cast<void*>(&tab[0])
Last edited on
Bumping this..

1
2
3
char tab_3[] = "abcd";
char* ptr = tab_3;
cout << *(ptr+4) //why doesn't this print the 0 character? 


How can you tell that it does not print the unprintable null value correctly?
I didn't know it was unprintable, so I guess that's the reason?!
Take a look at what character the number zero represents:

http://www.asciitable.com/

You can see it represents something called "NUL". Right at the start. The first 32 such, from "NUL" to "US" are not meant to be seen. Not meant for printing out.

Note that the '0' character is represented by the number 48. If the memory at location (prt+4) contained the number 48, and you sent that to cout as a char, you'd see '0' on the screen. The char '0' is not the same as the number 0. In memory the char '0' is represented as 48.
Last edited on
Topic archived. No new replies allowed.