char arrays and integer arrays... why the difference?

B

Bill Reyn

I am a Java programmer, a newbie with c++, struggling a bit...
Why does the code below return the starting address for an integer
array, but for a char array it does not return the starting address,
rather the actual total char array? Where's the address gone for the
char array pointer? It was OK for the int array.
Why the contradiction. What's the logic behind all this? Is it just a
compiler fudge?


#include <iostream>
using namespace std;

int main()
{
char l = 's';
int num[] = {1,2,3,4,5,6,7,8,9,10};
char* ch = {"Ritchie did this"};
cout << " ch is " << ch << " num is " << num << endl ;
return 0;
}

output is:

ch is Ritchie did this num is 0012F77C
Press any key to continue

-----------------------------------
If your interested in Cplusplus its:
void SwapInt( int &nA, int &nB)
{

int nC;
nC = nA;
nA = nB;
nB = nC;

}
 
J

JKop

Bill Reyn posted:
I am a Java programmer, a newbie with c++, struggling a bit...
Why does the code below return the starting address for an integer
array, but for a char array it does not return the starting address,
rather the actual total char array? Where's the address gone for the
char array pointer? It was OK for the int array.
Why the contradiction. What's the logic behind all this? Is it just a
compiler fudge?


#include <iostream>
using namespace std;

int main()
{
char l = 's';
int num[] = {1,2,3,4,5,6,7,8,9,10};
char* ch = {"Ritchie did this"};
cout << " ch is " << ch << " num is " << num << endl ;
return 0;
}

output is:

ch is Ritchie did this num is 0012F77C
Press any key to continue

-----------------------------------
If your interested in Cplusplus its:
void SwapInt( int &nA, int &nB)
{

int nC;
nC = nA;
nA = nB;
nB = nC;

}

That's nothing to do with C++ itself. It's to do with cout.

When cout receives a char*, it pressumes it's a string, hence:

cout << "Hello!!";

When it receives any other type of pointer:

int j;

cout << &j;

It'll print the address.

Here's a solution

cout << (void*)"Hello!!";

-JKop
 
R

Rolf Magnus

Bill said:
I am a Java programmer, a newbie with c++, struggling a bit...
Why does the code below return the starting address for an integer
array, but for a char array it does not return the starting address,
rather the actual total char array? Where's the address gone for the
char array pointer? It was OK for the int array.
Why the contradiction. What's the logic behind all this? Is it just a
compiler fudge?

No. Arrays of char are the traditional C way of storing strings. This is
supported in C++ too. Think about it. In your example program, you
write " ch is " to cout. But " ch is " is an array of char, and you want
cout to interpret that as a string and print that string. How is the
compiler supposed to know that it should handle ch differently?
OTOH, if you supply a pointer to int, there is nothing similar, therefore
there is no operator<< that takes a pointer to int. There is however one
that takes a pointer to void, and the pointer to int is converted into
that. This operator just prints the address that the pointer points to.
If you want the same to happen for a pointer to char, you have to
explicitly convert it to a pointer to void:

cout << " ch is " << static_cast<void*>(ch) << " num is " << num << endl ;
 
B

Bob Hairgrove

I am a Java programmer, a newbie with c++, struggling a bit...
Why does the code below return the starting address for an integer
array, but for a char array it does not return the starting address,
rather the actual total char array? Where's the address gone for the
char array pointer? It was OK for the int array.
Why the contradiction. What's the logic behind all this? Is it just a
compiler fudge?


#include <iostream>
using namespace std;

int main()
{
char l = 's';
int num[] = {1,2,3,4,5,6,7,8,9,10};
char* ch = {"Ritchie did this"};

Braces are not necessary here. Besides, it would be more correct to
declare ch as const char * (why doesn't BCC 5.5.1 give me a warning
here??)
cout << " ch is " << ch << " num is " << num << endl ;
return 0;
}

output is:

ch is Ritchie did this num is 0012F77C
Press any key to continue

The reason lies in the different overloads for operator<< in
std::eek:stream.

According to the C++ standard, arrays are implicitly changed to
pointers; presumably there is no overload for operator<< which takes
an int*, so the one for void* is used which prints the memory address
of the pointer. The overload for char*, however, prints the contents
of the character array up to the delimiting null byte (which is
implicitly added to a literal string).

The solution is to cast the [const] char* to a [const] void* (an
unsigned int should work, too), i.e.:

cout << " ch is " << reinterpret_cast<void*>(ch) << " num is "
<< num << endl ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top