pointers...trouble :(

ok...i am having serveer (<--forgive me for mispelling) difficulty in understanding pointers in the case that its been described...the "&" and the "*" confuse me on a bewildering scale :P if someone could, by any means, try to lighten my burden, it would be greatly appriciated ":D ty
closed account (oz10RXSz)
int x - declaration variable x of type int
int* y - declaration of variable y being a pointer to some variable of type int (pointer to int = int*)
int** z - declaration of variable z being a pointer to a pointer to some variable of type int
and so on

Now, when used in an expression:
x - variable of type int
y - pointer to int
*y - variable of type int pointed by y (you see, in the declaration you had int* y, but it can be also written as int *y, which means that *y must be of type int).
z - pointer to pointer to variable of type int
*z - pointer to variable of type int pointed by z
*(*z) or simply **z - variable of type int pointed by pointer pointed by z :D

*x - invalid, x is not a pointer, so you may not dereference it (* on the left side of a pointer means - take the value pointed by the pointer)
**y or *(*y) - invalid, y is a pointer, but *y is a variable of type int, not a pointer

The & thing is the simplest one. It simply creates a pointer to the thing that is afterwards:
&x - pointer to (the variable x of type int)
&y - pointer to (the pointer y to some int variable)
&z - pointer to (the pointer z to a pointer to int)

So now, when you know how to declare pointers, how to create them and how to use them, you can write:

1
2
3
int x = 5;     // simply a variable of type int
int* y = &x; // 2 things here: declare a new variable y, being a pointer to int and initialize it with a pointer to x
printf("%d\n", *y)  // prints 5 


To summarize: in expressions & is just the opposite than *. & returns a pointer to something.
* returns something pointed by a pointer. And * can be additionally used to declare pointers.


Last edited on
i THINK that i got it now :D just need a little practice...with the program code that i am currently testing with pointers i am trying to output the PLACE where x and y are stored, as well as the value contained within the x and y variables :P it would let me input the data for some reason...i KNOW this is probably something SO simple but... and help would be appriciated :D Thanks in advance!
1
2
3
4
int x=5;
int* px=&x;
cout << *px << endl; //prints value of x
cout << px << endl; //prints address of x 
Last edited on
here is what i have so far...

# include <iostream>
using namespace std;



int main ()
{
int x;
int *ptr1 = &x;
cin>>"enter a value for x ">> x;

int y;
int *ptr2 = &y;
cin>>"enter a value for y ">> y;

cout<<"the location of x is "<< ptr1 << " and the value contained within is "<< *ptr1 << endl;
cout<<"the location of y is "<< ptr2 << " and the value contained within is "<< *ptr2 << endl;
system ("pause");
return 0;
}
is there no way that i could input the data for the variable that the pointer is pointing to?
The only problem with that code is that you're trying to print something using cin.
Use cout to output text.
its still not letting me input anything even with the modifications to the code :P
here is what i have changed...

# include <iostream>
using namespace std;
int main ()
{
int x;
int *ptr1 = &x;
cout>>"enter a value for x ";
cin<< x;

int y;
int *ptr2 = &y;
cout>>"enter a value for y ";
cin<< y;

cout<<"the location of x is "<< ptr1 << " and the value contained within is "<< *ptr1 << endl;
cout<<"the location of y is "<< ptr2 << " and the value contained within is "<< *ptr2 << endl;
system ("pause");
return 0;
}
1
2
cout << whatever;
cin >> whatever;
You're mixing up input and output operators.
The data should flow in the directions the arrows point to.
Text to cout: cout << text
Value from cin to x: cin >> x
PERFECT! :D ty
Topic archived. No new replies allowed.