User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2436 | Replies: 9
Reply
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Help New to C++ Pointers and need help with identifying where the errors are

  #1  
Aug 25th, 2004
I am stuck on this problem..you are supposed to locate the error in the segment of code:

char var1, ptr1;
var1='X';
ptr1=&var1;

Can someone please help me figure this out. It is much appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: New to C++ Pointers and need help with identifying where the errors are

  #2  
Aug 25th, 2004
[quote=hfick]I am stuck on this problem..you are supposed to locate the error in the segment of code:

char var1, ptr1;
var1='X';
ptr1=&var1;

Can someone please help me figure this out. It is much appreciated.
I think their needs to be an * by the ptr1 where the variable is declared but I am really not sure.??
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: New to C++ Pointers and need help with identifying where the errors are

  #3  
Aug 25th, 2004
char var1, *ptr1;  // <--- * goes here
var1='X';
ptr1=&var1;

It's important to remember that you need '*' for each pointer variable. Therefore: char* ptr1, ptr2; doesn't work as intended.
For two ptrs, you must do char *ptr1, *ptr2.


Ed

[quote=hfick]
Originally Posted by hfick
I am stuck on this problem..you are supposed to locate the error in the segment of code:

char var1, ptr1;
var1='X';
ptr1=&var1;

Can someone please help me figure this out. It is much appreciated.
I think their needs to be an * by the ptr1 where the variable is declared but I am really not sure.??
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: New to C++ Pointers and need help with identifying where the errors are

  #4  
Aug 25th, 2004
Thank you very much
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Help Re: New to C++ Pointers and need help with identifying where the errors are

  #5  
Aug 25th, 2004
That is what I thought for the one above...But I am stuck again on this one..could you possibly help again?

char c='A';
char *p;
p=c

* goes here right?? char *c='A';
char *p;
p=c;

hopefully i am getting this..please let me know if this is right
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: New to C++ Pointers and need help with identifying where the errors are

  #6  
Aug 26th, 2004
char c='A';
char *p;
p=&c  // <-- should read a pointer (*) to char named p gets the the address (&) of char c
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Help Re: New to C++ Pointers and need help with identifying where the errors are

  #7  
Aug 26th, 2004
thank you..you must really get this stuff..must be nice...could you help me some more...

var1's address is 55441
int var1=2323;
int *ptr;
ptr=&var1;

var1=?
ptr=?
*ptr=?
Reply With Quote  
Join Date: Jul 2004
Location: India
Posts: 15
Reputation: shalin is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
shalin's Avatar
shalin shalin is offline Offline
Newbie Poster

Re: New to C++ Pointers and need help with identifying where the errors are

  #8  
Aug 26th, 2004
var1's address is 55441
int var1=2323;
int *ptr;
ptr=&var1;


var1 = 2323
ptr = 55441
*ptr = 2323
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Help Re: New to C++ Pointers and need help with identifying where the errors are

  #9  
Aug 26th, 2004
Thank you shalin....THat is what I thought but wasn't for sure...

THis one wants to know what the output produced is when the code is executed:

char var1 = 's';
char var2 = 'x';
char *ptr1, *ptr2;
ptr1=&var1;
ptr2=&var2;
*ptr2=*ptr1;
cout << *ptr1 << " " << var2 << endl;
Reply With Quote  
Join Date: May 2004
Posts: 251
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Rep Power: 6
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: New to C++ Pointers and need help with identifying where the errors are

  #10  
Aug 27th, 2004
Try to understand the concept of pointers rather.It's simple actually.A pointer points to the address in memory of a variable.That way if you change one, the changes can be seen at all places.Multiple pointer can point to a variable.

A var can be tought of as a TV and the pointer a Remote Control.Change the channel anywhere,the result is on the TV.And a TV can have multiple remotes

Look below:

&val means address of the variable var
*p (if is a pointer (int *p; )) means value at address pointed at by p.Changes this will change the variable it points to.

int val = 1;
p=&val;
val+=10

cout<<val<<" *p="<<*p; //output is the same for both
(*p) += 4; //changes the value of val
cout<<val<<" *p="<<*p; //output is the same for both

The outputs are:
11 11
15 15

*(p++) increses the address to which it points to by the pointers datatype size.
*(p)++ increses the val at the address pointed by p by 1.

Understand the pointer concept ?
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC