Thread: Exercise - pointer

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    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 :
    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 ?

    my answer :

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

    Anyway, is it relevant my answer?
    Last edited by khelkely; 10-17-2013 at 08:57 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of asking us if you are right, write programs to find out. If you don't understand why (or if you suspect undefined behaviour), that's when you should ask us
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointer to function exercise need advice
    By A34Chris in forum C Programming
    Replies: 4
    Last Post: 03-11-2012, 11:50 PM
  2. Compiler give error when run Pointer exercise .
    By ToNy_ in forum C Programming
    Replies: 16
    Last Post: 01-03-2012, 04:41 AM
  3. Help with a pointer exercise in the C programming language
    By cb0ardpr0gr^mm3r in forum C Programming
    Replies: 2
    Last Post: 11-07-2010, 01:56 AM
  4. exercise with pointer and structs
    By xphoenix in forum C Programming
    Replies: 4
    Last Post: 06-29-2010, 09:46 AM
  5. Need help on this exercise
    By tiachopvutru in forum C++ Programming
    Replies: 12
    Last Post: 05-19-2008, 11:23 PM