473,657 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1811
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<<end l;
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<<end l;
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<voi d*>(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
4950
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
5563
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. I have a line like this: "329:47 InitGame: \.Admin\xxx\.Website\www.yyy.de\g_gametype\hq\gamename\Call of
17
4901
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 the period in the email address is the only part I am having problems with. I have posted part of my code below. Any help would be greatly appreciated. THANKS !
3
1699
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 computer will show those five characters back to the user. So far I have: #include <iostream.h> int main() { char* buffer = " "; //Five spaces
4
1571
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 varchar value 'danwet w125 ' to a column of data type int. I have declared @Sellername as Varchar. Please can someone tell me what I'm
2
2230
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 >> find what is considered to be the latest ansi/iso C spec. I cannot >> even find C99 in its final draft. Where in ansi.org or the like do >> I find it? > > The official C99 specification is copyright ISO and distributed by > various national...
3
2775
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 which is incorrect.. I have no clue how to fix it and i'm struggling for two days already :( On Mar 13, 3:09 pm, "Tiago Halm" <th...@nospam.hotmail.comwrote:
3
2206
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 that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another menu appears with options based on that city. I can't seem to get the input from these fields in the...
5
13348
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
8394
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
8306
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
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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
7327
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...
0
4152
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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.