# Exercise - Pointer

• October 17th, 2013, 09:32 PM
khelkely
Exercise - Pointer
Q1: Trace the partial of program below:

1. int v = 8, *r, *s;
2. int *p;
3. int q = 100;
4. p = &q;
5. r = p;
6. *p = 20;
7. p = new int;
8. *r = 30;
9. q = v;
10. s = p;
11. *s = 50;

What are the last values of *p, q, *r, v and *s?
*p = 20
q = ?
*r = 30
v = ?
*s = 50

i can't find q and v. But, if q will point whatever v point, so, the value of q is 8 ? am i right ?

Q2 = Given the following codes:
1. int *p , *q , v , nom[5];
2. p = &v;
3. *p = 12;
4. q = p;
5. nom[0] = *q;
6. p = nom;
7. p++;
8. nom[2] = 12;
9. *p = 13;
10. *q = 10;
11. v = 11;
12. *(p+3) = 16;
13. p = &nom[3];
14. *p = 10;
15. p--;

What are the last values of *p, *q, v and nom ?

*p = 10
*q = 10
v = 11
nom = ?

i can't find nom .

Q3 : Given below declaration :
Code:

```struct Node {       int  value;       Node  * next;  };```
In the main function, the following codes are given:
1. Node *q, *r;
2. Node *p = new Node; //address of this new Node adalah 10000
3. q = r = NULL;
4. p -> value = 9;
5. p -> next = NULL;
6. q = new Node; //address of this new Node adalah 10050
7. p->next = q;
8. p->next->value = 8;
9. q->next = new Node; //address of this new Node adalah 10100
10. r = q;
11. r-> value = 10;
12. q->next->value = 11;
13. q = q-> next;
14. q -> next = NULL;

What are the last values of p->value and q->value ?

p->value = 9
q->value = NULL

Anyway, is it relevant my answer?
• October 17th, 2013, 10:11 PM
laserlight
Re: Exercise - Pointer
I suggest that you pose your questions to one particular community. If you have received no satisfactory answer after a considerable time, then you move on to another community, noting that you previously asked elsewhere.

Anyway, my answer is the same: write a program to find out. Ask if you don't understand why the results are what they are.
• October 18th, 2013, 02:56 AM
Arjay
Re: Exercise - Pointer
Quote:

Originally Posted by laserlight
write a program to find out. Ask if you don't understand why the results are what they are.

You know, it might not seem like it, but this is the best answer.

If you write a program and spend a couple minutes stepping through the program in a debugger, you can see the exact values of all the variables.

Debugging a program may sound hard but the basics are very easy to learn. If you don't how, please ask.
• October 18th, 2013, 05:02 AM
2kaud
Re: Exercise - Pointer
Being able to debug a program and trace its execution path and inspect variables is an essential part of becoming a programmer. You need to become familiar with using the debugger and tracing through small programs like this is an excellent way to start. As Arjay said in post #3, if you come across problems using the compiler/linker/debugger, just ask us.