Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

array differences & anomalies

I've been knocking my brains out with arrays (char & int), pointers, and references used as parameters in functions.

Can anyone give me a clear explanation as to why integer arrays and character arrays behave differently?

I understand that an array name is essentially a constant pointer . . . which means, or should mean that the array name should return an address to the first element in the array, however, why in the following code segment does &integer not return the same address as &integer[0].

Additionally, the call to cout << "characters" reports the entire character array. But the call to cout << "integers" only reports the first element. I know there has to be a really good reason for this seeming anomaly, but damned if I can see it.

I have cruised the internet for weeks looking for a clear explanation and just have not found it . . . in any case, thanks in advance for any help you might offer.

Code follows:

//pass integer/character array to function

#include <iostream>

void int_function (int integers[])
{
cout << integers[0] << "\n"; // 5
cout << integers << "\n"; // &integers[0]
cout << &integers << "\n\n"; // &integers
}

void char_function (char characters[])
{
cout << characters[0] << "\n"; // D
cout << characters << "\n"; // Dante
}

void main ()
{
int ints[] = {5, 10, 15};
int_function (ints);

char string[] = "Dante";
char_function (string);
return;
}
[1654 byte] By [amalafrida] at [2007-11-11 6:43:05]
# 1 Re: array differences & anomalies
I've been knocking my brains out with arrays (char & int), pointers, and references used as parameters in functions.

Can anyone give me a clear explanation as to why integer arrays and character arrays behave differently?

I understand that an array name is essentially a constant pointer . . . which means, or should mean that the array name should return an address to the first element in the array, however, why in the following code segment does &integer not return the same address as &integer[0].

&integer and &integer[0] are not the same thing. &integer[0] returns, as expected, the address of the first element, which is equivalent to integer. However, &integer is the address of an address, i.e., an address that holds the address &integer[0].


Additionally, the call to cout << "characters" reports the entire character array. But the call to cout << "integers" only reports the first element. I know there has to be a really good reason for this seeming anomaly, but damned if I can see it.

char arrays are a special case. They typically represent C-strings, so cout has an overloaded version that takes char * arguments and treats them as strings. integer arrays represent nothing but arrays of integers so they don't get any special treatment. Besides, what should cout<<integers do except for diaplying the array's adddress?
Danny at 2007-11-11 21:03:00 >
# 2 Re: array differences & anomalies
Thanks for the response. Yes, last night didn't get a lot of sleep thinking about this. My conclusion matches your response pretty closely.

To ask for &integer is to ask for an address of an address (same thing with &characters) . . . which doesn't make much sense . . . I assume that the compiler is simply reporting 'garbage' when I pose it this question.

Thanks again for your concise & accurate response.

R. Mullin :WAVE:
amalafrida at 2007-11-11 21:03:56 >