473,770 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

displaying address of a character array ?

hi,

If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.

thanks.

Apr 25 '06 #1
9 3739
Wondering Wanderer wrote:
If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr
or cout << &arr displays the entire string in the array, not the
address.


cout << static_cast<voi d*>(arr);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '06 #2
Wondering Wanderer wrote:
If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.


That's because << overloads to operator<<(char *), which you don't need.

To get operator<<(unsi gned long), you must typecast:

cout << "0x" << hex << reinterpret_cas t<unsigned long>(arr);

I switched to hexadecimal to make the bit patterns in the pointer clearer.

Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Apr 25 '06 #3
Wondering Wanderer wrote:
hi,

If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.


Try cout << static_cast<voi d*>(arr)
Apr 25 '06 #4
thanks victor, phlip and rolf. there's another typecasting way too :
cout << (int *) arr ;

Apr 26 '06 #5

Phlip wrote:
Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);
This conversion should require a reinterpret_cas t. VC++7.1 compiles it
with warnings though if one specifies compiler option /Wp64. This means
that the cast may change the value on a 64bit system.

typedef unsigned long long ULong;//...

....seems to make this warning go away.

see c++ standard98:

5.2.10/3 - mapping of reinterpret_cas t implementation defined
5.2.10/4 - A pointer can be explicitly (not implicitly) converted to
any integral type large enough to hold it.

Regards,

Werner

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


Apr 26 '06 #6
werasm wrote:
Phlip wrote:

Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);

This conversion should require a reinterpret_cas t.


A C-style cast can do a reinterpret_cas t. By 5.2.3, that code is
equivalent to:
cout << "0x" << hex << (ULong)arr;
which is equivalent to:
cout << "0x" << hex << reinterpret_cas t<ULong>(arr);

Tom
Apr 26 '06 #7

Tom Widmer wrote:
werasm wrote:
Phlip wrote:

Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);

This conversion should require a reinterpret_cas t.


A C-style cast can do a reinterpret_cas t. By 5.2.3, that code is
equivalent to:


Yes, but that was not a c-style cast :-). That was instantiating a
temporary of type unsigned long...

Big difference between ULong(arr) and (ULong)arr, not true?

W
cout << "0x" << hex << (ULong)arr;
which is equivalent to:
cout << "0x" << hex << reinterpret_cas t<ULong>(arr);

Tom


Apr 26 '06 #8
werasm wrote:
Tom Widmer wrote:
werasm wrote:
Phlip wrote:

Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);
This conversion should require a reinterpret_cas t.


A C-style cast can do a reinterpret_cas t. By 5.2.3, that code is
equivalent to:

Yes, but that was not a c-style cast :-). That was instantiating a
temporary of type unsigned long...

Big difference between ULong(arr) and (ULong)arr, not true?


No, the two are semantically identical - see 5.2.3 in the C++ standard.
For example:

typedef int* p;
int i = 1;
p pi = p(i); //meant to do p(&i)

Sadly, that is well-formed C++.

Tom
Apr 26 '06 #9

Tom Widmer wrote:
No, the two are semantically identical - see 5.2.3 in the C++ standard.
For example:

typedef int* p;
int i = 1;
p pi = p(i); //meant to do p(&i)

Sadly, that is well-formed C++.
Thanks (I've learnt), Yes truly sad (honestly - why?). In that case I'd
rather use:

cout << "0x" << hex << reinterpret_cas t<ULong>(arr); //...or better
std::cout << "0x" << std::hex << static_cast<voi d*>(arr) << std::endl;

Werner

Tom


Apr 26 '06 #10

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

Similar topics

12
1357
by: Don | last post by:
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b" is a space character). In other words, I always want the second digit of each number to be in the same column, so that the beginning of "restOfText" will start in the same column for each record produced by the following code. I've tried...
117
11894
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
15
2149
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added automatically to the end of the string. Let say char name="123456789" If entered the name in a loop;
0
1460
by: Coco | last post by:
Hi! I have been searching for solution for the problem i am facing in displaying chinese character in my aspx page initially when i created the aspx page with some chinese chaarcter, it worked perfectly, but i am having problem when setting the label of my page to some chinese character during run-time which i do it from the code behind i realize the chinese will displayed correctly if i opened
1
2590
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would like to be able to read this data and put each row into a variable? so I could display the values in a web page where I want.
12
2344
by: korund | last post by:
How to make javascript alert with non-english text displaying correctly on computers where english only is default system & language settings? For web page the solution is just use meta tags: <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> Will this work for javascript alerts also?
2
1711
by: lakepeir | last post by:
The return key is a special character. I'm trying to display the return key by coping the special character into a character array. When I run my app, the return key (arrow) does not display. Can someone offer advice on displaying return, backspace, etc? Thanks.
7
1713
Chrisjc
by: Chrisjc | last post by:
have one page that has a drop down menu, once the user selects the desired option it will then pass the selected value to a 2nd drop down. Once they select there next option I pass that value... Now I would like to hold the 2nd selected value have it search the database find all that match the selected and then only display what matches it in the corresponding ROWS in the database. I was hoping to get some help with an array because I...
17
2326
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.