473,403 Members | 2,338 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,403 software developers and data experts.

trouble outputing the address location of a character

Using Visual C++2005 Expression Edition

Having trouble outputting the value referenced by the pointer and then
outputing the address of the pointer for the char. Any ideas would be
greatly appreciated!

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
const char* val_4 = "apple juice";

//Right now, pval contains no particular val.

int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char pval_4 = val_4[6];

//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}

Aug 25 '06 #1
4 1792
jdcrief wrote:
Using Visual C++2005 Expression Edition

Having trouble outputting the value referenced by the pointer and then
outputing the address of the pointer for the char. Any ideas would be
greatly appreciated!
//Right now, pval contains no particular val.

int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char pval_4 = val_4[6];
at first, pval_4 it's not a pointer!
cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;
secondly, you are unlucky: operator<< for char* is overriden, so you
have to cast to another type of pointer in rder to display it correctly,
like (void*).

Bye,

Zeppe
Aug 25 '06 #2
jdcrief wrote:
[..]
//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}
When outputting the addresses cast them all to (void*). The problem
with 'char const*' is that the output operator (the <<) treats it
differently than many other pointers. It tries to output the C string
behind that pointer. If you cast it to void*, the stream will have no
preconceived notion of what the address is of, it will simply output
the pointer value (in hex, probably).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #3
i've decided to go in a different direction....i am stil stuck on
producing the correct output for pvalue. am i close?

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
char value[] = { 'A', '\0' };

//Right now, pval contains no particular val.
int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char* pvalue = value;
//Now, val in this c++ program contains the memory address of the
variable pval
cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;
cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is - "<<pval_3<<endl;

cout<<"Address of the Char value "<<value<<" is - "<<*pvalue<<endl;
return (0);
}

Victor Bazarov wrote:
jdcrief wrote:
[..]
//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}

When outputting the addresses cast them all to (void*). The problem
with 'char const*' is that the output operator (the <<) treats it
differently than many other pointers. It tries to output the C string
behind that pointer. If you cast it to void*, the stream will have no
preconceived notion of what the address is of, it will simply output
the pointer value (in hex, probably).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 27 '06 #4
jdcrief wrote:
i've decided to go in a different direction....i am stil stuck on
producing the correct output for pvalue. am i close?

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
char value[] = { 'A', '\0' };

//Right now, pval contains no particular val.
int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char* pvalue = value;
//Now, val in this c++ program contains the memory address of the
variable pval
cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;
cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is - "<<pval_3<<endl;

cout<<"Address of the Char value "<<value<<" is - "<<*pvalue<<endl;
Victor Bazarov already answered your question, but you ignored him.
Change the above line to the following:

cout << "Address of the char array value "
<< value
<< " is: "
<< static_cast<void*>(pvalue)
<< endl;

Best regards,

Tom

Aug 27 '06 #5

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: Thorsten Walenzyk | last post by:
Hi all, I'm pretty new to PHP. So far I know regular expression only from Perl I've written a perl script which I now want to port to PHP. It works in PErl, but gives my a headache in PHP. ...
17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
3
by: Ian Arnold | last post by:
Alright, so I know that you can use char pointers to store strings, and I'm trying to make a very simple program to see how it all works that will have the user enter 5 characters, then the...
4
by: Driesen via SQLMonster.com | last post by:
Hi guys I having trouble with this sproc. I get the following error when testing: Server: Msg 245, Level 16, State 1, Procedure UTL_CompletenessCheckLoan, Line 231 Syntax error converting the...
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
3
by: kkao77 | last post by:
can you show me in more detail? I have same problem where on the page it's https://service.premilance.com/Company.svc, but the svcutil tells me to get it from https://pserver1/Company.svc?wsdl...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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
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...
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,...

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.