6

What is the difference between int and char arrays below:

int main()
{
    int numbers[] = {2,1,3};
    char letter[] = {'a','b','\0'};
    cout<< numbers<<endl;
    cout<< letter<<endl;
}

Output:

0x22ff12 // an address
ab

Why isn't the 213 displayed ? I know the name of an array will point to the address of its first element, but why does a char array display different behavior?

SH4D0WS1N
  • 23
  • 1
  • 4
S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58
  • 4
    In C, arrays decay into pointers to their first element in most contexts ... but `cout` and `<<` and `endl` are unrecognized elements in the C language. – pmg Nov 25 '11 at 16:43
  • Well ... I mean: the `<<`, in C, is the bitwise shift left operator and it cannot be used with unrecognized language elements :) – pmg Nov 25 '11 at 16:50
  • Does this answer your question? [cout << with char\* argument prints string, not pointer value](https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) – xskxzr Dec 29 '22 at 06:45

10 Answers10

11

There is no operator<< overload that takes arrays, exactly, so the arguments you pass (eg numbers and letter) undergo array-to-pointer conversion, to void* and char* respectively.

There is an overload of operator<<() that takes a const void*, and another that takes a const char*. When you call:

cout<< numbers<<endl;

the const void* version is matched, but when you call:

cout<< letter<<endl;

the const char* version is matched.

In the const void* version, the pointer is displayed, while with the const char* version, the string is displayed up to the null terminator.

dlev
  • 48,024
  • 5
  • 125
  • 132
John Dibling
  • 99,718
  • 31
  • 186
  • 324
9

When you print an array with cout it will print the base address of the array.

The exception is with char arrays which have been overloaded to print it as a c-string.

If you want to print the elements of the int array, you need to do it element-by-element.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
6

The reason is thatoperator<< overloaded for const char* which prints each character till it encounters \0.

There is no such overload corresponds to int[N] which prints each element in it. Instead when you write cout << numbers, it invokes operator<< which is overloaded for void*, and which prints the address.

However, if you overload operator<< for T[N], then you can print it like that as well.

Here is a simple illustration:

template<typename T, size_t N>
std::ostream & operator<<(std::ostream & out, const T (&a)[N])
{
   for(size_t i = 0 ; i < N ; ++i)
      out << a[i] << ' ';
   return out;
}

int main()
{
   int numbers[] = {2,1,3};
   char letter[] = {'a','b','\0'};
   cout<< numbers<<endl;
   cout<< letter<<endl;
}

Output:

2 1 3 
a b

Demo : http://ideone.com/O4T9N

Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

In C, and therefore in C++, a string is often represented by an array of chars terminated in a 0. Therefore an overloaded operator<< is provide for the class std::ostream, of which std::cout is an instance , which prints the char* as a string. There is no such common use of int arrays, nor any convention so the operator would 'know' how many elements to output, so the pointer to the array is matched to the version of operator<< which outputs any other pointer by printing its address.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • +1 because this is what I just wanted to write. But I think it would be educational (= helpful) if you could show the concrete function prototypes of the two overloads. – Konrad Rudolph Nov 25 '11 at 16:46
3

char arrays are special because there is an overload for operator << that displays the content as a string.

All other arrays will have the address displayed by default.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

In C/C++ an array is in fact a pointer to the first element. A pointer holds the address where a value is stored. Therefore, if you print the pointer numbers, you will get the address where the first value (2) is stored in memory.

char* is an exception, as it will behave as a string when you try to print it.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • 2
    Arrays aren't pointers; they *decay* to pointers in a number of contexts, but that doesn't make them the same thing. – dlev Nov 25 '11 at 16:57
1

Your code mostly refers C. An array of char is de-facto representation of strings in C. On the other hand, an array in C is also a pointer to the memory cell (an address of the cell) that holds the first element of the array.

So, when you print out an array of characters, you in fact print out a string (because C treats it that way). When you're printing an array of integers, you're printing out the address of the first element of the array.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
0

A char array contains characters.
It can be initialized like:

char arr[4]={'a','b','c','\0'};
char arr[4]={"abc"};

An integer array contains integers.
It can be initialized like:

int arr[4]={1,2,3,4};  
AleXelton
  • 767
  • 5
  • 27
0

numbers is a pointer. All arrays in C++ are in fact pointers, numbers[3] just means "the value at the memory address &number+3", so you're outputting the memory address of the first element in numbers.

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
0

There is no reason the compiler should know where your int[] array ends, but tradition and standard libraries dictate that C strings are null terminated char[] arrays. There is no such tradition or library support for null terminated int[] arrays.

There are C++ pretty printers templates available if you need this functionality. I vaguely recall that one employs an array's bound when the type actually knows the bound, i.e. your code still won't work since you use [] not [3].

Just fyi, your code cannot be fixed by replacing the [] with a [3] inside the STL, although perhaps operator<< could be overloaded.

Jeff Burdges
  • 4,204
  • 23
  • 46
  • There is some care required in using C array bounds for template parameters : http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc16deduct_non-type.htm – Jeff Burdges Nov 25 '11 at 17:07