Help with homework!

Is this valid or not? Explain. If correct what will be values of variables involved:


Code

int x = 15;
int *xp = x;
int *yp = *x;
int *zp = &x;
int *wp = new int;
wp = 5;
*wp = 5;
&wp = 5;
xp = wp;
*zp = 17;
float *fp = new float;
fp = wp;
*fp = (int) *wp;
zp = &x;
float *fq = 5;


Appreciate all the help I can get
Well, how far have you gotten?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int x = 15;
int *xp = x;
int *yp = *x;
int *zp = &x;
int *wp = new int;
wp = 5;
*wp = 5;
&wp = 5;
xp = wp;
*zp = 17;
float *fp = new float;
fp = wp;
*fp = (int) *wp;
zp = &x;
float *fq = 5;
this is the last 1/3 of the assignment i have. not that great with c++, so i dont know much about this part of the assignment
1
2
int x = 15;
int *xp = x;


That's nonsensical. x is an int. It's silly to say a pointer is equal to an int.

Learn about pointers. here is a very simple explanation: http://cplusplus.com/articles/EN3hAqkS/
Here is another one: http://cplusplus.com/articles/z186b7Xj/
anyone else have anymore input on this?
Did you read Moschops's article?

Note that C++ is statically typed so you can only assign a value to a variable of the same type. If a variable is of type int* then you can't assign an int to it (without casting). A variable of type T* is a pointer to type T. i.e. float* is a pointer to a float. The & in the above examples is the "address of" operator. If x is an int, then &x is the address of x, which is a pointer to x, which is of type int*.
anyone else have anymore input on this?


Seriously, just read what a pointer is and then you'll be able to answer the whole thing.
Topic archived. No new replies allowed.