Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 01:35 PM
Bill Reyn
Guest
 
Posts: n/a
Default char arrays and integer arrays... why the difference?

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;

}

  #2  
Old July 22nd, 2005, 01:35 PM
JKop
Guest
 
Posts: n/a
Default Re: char arrays and integer arrays... why the difference?

Bill Reyn posted:
[color=blue]
> 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;
>
> }
>[/color]

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
  #3  
Old July 22nd, 2005, 01:35 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: char arrays and integer arrays... why the difference?

Bill Reyn wrote:
[color=blue]
> 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?[/color]

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 ;

  #4  
Old July 22nd, 2005, 01:35 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: char arrays and integer arrays... why the difference?

On 22 Jun 2004 04:42:42 -0700, breyn12345@hotmail.com (Bill Reyn)
wrote:
[color=blue]
>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"};[/color]

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??)
[color=blue]
> 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[/color]

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

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 ;


--
Bob Hairgrove
NoSpamPlease@Home.com
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.