Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char arrays and integer arrays... why the difference?

Reply
Thread Tools

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

 
 
Bill Reyn
Guest
Posts: n/a
 
      06-22-2004
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;

}
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      06-22-2004
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-22-2004
Bill Reyn wrote:

> 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 ;

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-22-2004
On 22 Jun 2004 04:42:42 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Bill Reyn)
wrote:

>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: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 ;


--
Bob Hairgrove
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused between char and char* and connection to Arrays arnuld C++ 19 03-30-2007 04:42 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char->integer, integer->char commands christophergraber@gmail.com Perl Misc 5 11-22-2005 07:13 AM



Advertisments