0

can i change the value of an int with a pointer. e.q.


int n = 20;


int *size = n;

can i change n by doing this *size++.


thanx

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by sweetyk2
0

No. This will increment the pointer first and then deference the result. You would require paretheses here to achieve the result you want.

(*size)++

This is because unary unary operators like ++ associate right to left..

Also you have a problem in that the expression

int *size = n;

is probably not doing what you intend. this is setting the pointer to an adress of 0x00000014 (20), not pointing it at n as you seem to desire. You need to use the address of operator to set size pointing to n.

int *size = &n;
-1

exactly,This will increment the pointer first and then deference the result. You need paretheses

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.