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?
my answer :
*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 ?

my answer :
*p = 10
*q = 10
v = 11
nom = ?

i can't find nom .

Q3 : Given below declaration :
1
2
3
4
5
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 ?

my answer :

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

Anyway, is it relevant my answer?
Last edited on
Q1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int v = 8, *r, *s;  // v = 8, r = undefined, s = undefined
    int *p; // p = undefined
    int q = 100; // q = 100
    p = &q; // p points to q.  *p = 100
    r = p; // r points to q. *r = 100
    *p = 20; // *p = *r = q = 20
    p = new int; // p points to something new and undefined, *r = q = 20 still
    *r = 30; // *r = q = 30
    q = v; // *r = q = v = 8
    s = p; // s points to the same memory as p, both are undefined
    *s = 50;  // *s = *p = 50

    std::cout << "*p = " << *p << std::endl
              << " q = " <<  q << std::endl
              << "*r = " << *r << std::endl
              << " v = " <<  v << std::endl
              << "*s = " << *s << std::endl;
}
*p = 50
 q = 8
*r = 8
 v = 8
*s = 50
Q2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
    int *p , *q , v , nom[5]; // all undefined
    p = &v; // *p = v = undefined
    *p = 12; // *p = v = 12
    q = p;  // *q = *p = v = 12
    nom[0] = *q; // nom[0] gets 12
    p = nom; // p = &nom[0], *p = 12
    p++; // p = &nom[1]
    nom[2] = 12;
    *p = 13; // *p = nom[1] = 13
    *q = 10; // *q = v = 10
    v = 11; // *q = v = 11
    *(p+3) = 16; // p+3 = &nom[1+3].  nom[4] = 16
    p = &nom[3]; // *p = undefined
    *p = 10; // *p = nom[3] = 10
    p--; // p = &nom[2]. *p = nom[2] = 12

    std::cout << " *p    = " <<  *p    << std::endl
              << " *q    = " <<  *q    << std::endl
              << "  v    = " <<   v    << std::endl
              << "nom[0] = " << nom[0] << std::endl
              << "nom[1] = " << nom[1] << std::endl
              << "nom[2] = " << nom[2] << std::endl
              << "nom[3] = " << nom[3] << std::endl
              << "nom[4] = " << nom[4] << std::endl;
}
 *p    = 12
 *q    = 11
  v    = 11
nom[0] = 12
nom[1] = 13
nom[2] = 12
nom[3] = 10
nom[4] = 16
12. q->next->value = 11;
13. q = q-> next;


Doesn't q point to a Node, whose value is set?
Last edited on
Q3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

struct Node
{
    int value;
    Node* next;
};

int main()
{                       // p->next | p->value | q->next | q->value | r->next | r->value
    Node *q, *r;        //                         -         -          -         - 
    Node *p = new Node; //  uninit    uninit       -         -          -         - 
    q = r = NULL;       //  uninit    uninit       -         -          -         - 
    p->value = 9;       //  uninit       9         -         -          -         - 
    p->next = NULL;     //   NULL        9         -         -          -         - 
    q = new Node;       //   NULL        9       uninit    uninit       -         - 
    p->next = q;        //    q          9       uninit    uninit       -         - 
    p->next->value = 8; //    q          9       uninit      8          -         - 
    q->next = new Node; //    q          9       uninit      8          -         - 
    r = q;              //    q          9       uninit      8        uninit      8          
    r->value = 10;      //    q          9       uninit      10       uninit      10
    q->next->value = 11;//    q          9       uninit      10       uninit      10
    q = q->next;        //    q          9       uninit      11         q         10
    q -> next = NULL;   //    q          9        NULL       11         q         10

    std::cout << "p->value: " << p->value << std::endl
              << "q->value: " << q->value << std::endl
              << "r->value: " << r->value << std::endl;
}
p->value: 9
q->value: 11
r->value: 10
Last edited on
Topic archived. No new replies allowed.