943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3233
  • C++ RSS
Aug 25th, 2004
0

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

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 25th, 2004
0

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

[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.??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 25th, 2004
0

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

C++ Syntax (Toggle Plain Text)
  1. char var1, *ptr1; // <--- * goes here
  2. var1='X';
  3. 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]
Quote 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.??
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 25th, 2004
0

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

Thank you very much
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 25th, 2004
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 26th, 2004
0

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

C++ Syntax (Toggle Plain Text)
  1. char c='A';
  2. char *p;
  3. p=&c // <-- should read a pointer (*) to char named p gets the the address (&) of char c
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 26th, 2004
0

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

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=?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 26th, 2004
0

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

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


var1 = 2323
ptr = 55441
*ptr = 2323
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shalin is offline Offline
15 posts
since Jul 2004
Aug 26th, 2004
0

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

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 27th, 2004
0

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

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 ?
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Input a "password" string
Next Thread in C++ Forum Timeline: Major problem with C++ compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC