| | |
New to C++ Pointers and need help with identifying where the errors are
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2004
Posts: 18
Reputation:
Solved Threads: 0
[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.??
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.??
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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.
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
?
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
? ![]() |
Other Threads in the C++ Forum
- Previous Thread: *Pointer program problem.
- Next Thread: Major problem with C++ compiler
Views: 2939 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





