473,462 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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;

}
Jul 22 '05 #1
3 1674
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
Jul 22 '05 #2
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 ;

Jul 22 '05 #3
On 22 Jun 2004 04:42:42 -0700, br********@hotmail.com (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::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
No**********@Home.com
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
42
by: sarathy | last post by:
Hi, I need clarification regarding signed characters in the C language. In C, char is 1 byte. So 1. Unsigned char - ASCII CHARACTER SET - EXTENDED CHARACTER SET
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
19
by: arnuld | last post by:
i am trying to understand arrays and char. Stroustrup says, this a string literal: "this is a string literal" he says it is of type /const char/. he also says, because of keeping...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.