473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

output a string's address

I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";
char *p;
p=str;
//output "ABCD" address.why is (long)p different from (void *)p ?
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.

Oct 22 '05 #1
9 2424
ting wrote:
I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";
This is deprecated. Initialising a pointer to non-const char with
a literal creates a dangerous illusion that you are allowed to change
the contents of the memory. It's a C-ism. You should start to drop
those habits.
char *p;
p=str;
This is not even a C-ism. Why declare and then assign when you can
simply initialise

char *p = str;

?
//output "ABCD" address.why is (long)p different from (void *)p ?
How is it different? One is output as decimal and the other as hex?
That's the way cout outputs numbers versus pointers.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;
The C-style casts are another thing you should abandon.

return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.


Well, next time tell us _how_ it is different, and we will make a guess
why that might be.

V
Oct 22 '05 #2

"ting" <ti*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";
char *p;
p=str;
//output "ABCD" address.why is (long)p different from (void *)p ?
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.


A pointer is not an integer. An integer is not a pointer.
Why do you believe differently?

-Mike
Oct 22 '05 #3
thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

Oct 24 '05 #4
thank you anyway.i think the two ways can output a pointer's address, i
only concerns why they are different when changed to decimal or hex.

Oct 24 '05 #5
ting wrote:

thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;


Can you show what output you get, or is this a secret?

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 24 '05 #6
* ting:
thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;


The values are not different, but their presentation might be.

Try

cout<<hex<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

And don't respond "still different": as a minimum, include your output, what
you think is different, and info about your compiler.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 24 '05 #7

"ting" <ti*******@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
thank you anyway.i think the two ways can output a pointer's address, i
only concerns why they are different when changed to decimal or hex.


Are you aware that, for example, the
values 0x0A and 10 (decimal) are *not*
different, but exactly equal?

-Mike
Oct 24 '05 #8
certainly. i know that.

Oct 25 '05 #9
thank anyone who concerns the topic. i knew the result.
i mis-calculated the address. sorry!

Oct 25 '05 #10

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

Similar topics

5
by: Abe Simpson | last post by:
Hello all, The application I am working on must never output numbers in a floating-point format, that is, something like 2e-002 is a big no-no. At the same time, it must output numbers in a...
4
by: Mike Conmackie | last post by:
Hi Folks, I've probably omitted something very basic but I have no idea what it might be. The results of my transformation _should_ be an xml file but all I get is the xml declaration...
3
by: Sid | last post by:
Hi folks, I wrote this code to test if I get the same address for all the variables in a union but for some reason the address I got when I used a char variable in a union seems bizarre- can...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
1
by: Holly | last post by:
Hi, I have a page (A) that allows users to enter addresses and displays direction information and map images. The page A calls Microsoft's mappoint web service, gets the route info and map image....
3
by: bloc | last post by:
I am programming an interactive CV using xml, xslt and java script. The page consists of a header which contains links to various 'sections' on the xml cv, a left and right menu, and a central...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
19
by: Serman D. | last post by:
Hi, I have very limited C knowledge. I want to convert to output from a MD5 hash algorithm to printable ascii similar to the output of the md5sum in GNU coreutils. Any help on how to do the...
8
by: victor.herasme | last post by:
Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.