CS16, 10W, UCSB—H11: (Addresses, Pointers, Arrays, Etter, Section 6.1) Total points: ? (printable PDF)

Available online at: http://www.cs.ucsb.edu/~pconrad/cs16/10W/homework/H11

Accepted: on paper, in Lecture (11am Thursday Feb 11th)

Late Policy: No email submission allowed—and don't "slip it under my door". If you need to make it up, you must do so during office hours, or make an appointment to see me, and you must request this appointment within 48 hours of when the assignment was originally due.

Personal Day/Sick Day policy: Everyone is permitted one "personal day/sick day" when you get to make up a missed homework assignment for free during office hours or via appointment. After that, you may not make up the homework assignment—you can only earn back the points through extra credit opportunities.       
(For more details, see the syllabus and the homework policy)


Name: (3 pts)___________________________  UMail address (4 pts)  ______________@umail.ucsb.edu

Lab Section (3 pts) Circle one:         3pm       4pm         5pm           unknown


This assignment is due IN Lecture on Thursday.
It may ONLY be turned in during Lecture on Thursday.
Do NOT turn it in early to your TA on Tuesday in lecture,
or late to your TA in Lab on Thursday .

Name of your pair programming partner(s), if you work together:


Read Section 6.1and 6.2 in your Etter textbook—and review your lecture notes from Thursday Feb 4.

In addition, keep in mind that in the textbook, Etter uses %u as the format specifier for pointers, while in lecture I tend to use %p instead.

The difference is that %u prints the address value as an "unsigned integer", in base 10, while %p prints the address value in hexadecimal.

Then answer these questions:


  1. p. 285 shows the program chapter6_1 and p. 285 shows the program chapter6_2

    (In the online version of this homework, you can click on those names as links and get access to the source code in case you want to copy and paste the code and run it.)


    1. (5 pts) p. 284 in your book shows sample output from the program chapter6_1:
      a = 1; address of a = 1245052
      b = 2; address of b = 1245048

      My question to you is this: if you ran this program on CSIL, would the output be the same or different? If different, which parts would be the same, and which parts would be different? And most importantly, explain why.

      (Hint: the answer I'm looking for can be found somewhere in the textbook on pages 284-285.)






    2. (5 pts) Same question, but for the sample output from the program chapter6_2

      a = -858993460;  address of a = 1245052
      b = -858993460; address of b = 1245048
      (Hint: again, the answer I'm looking for can be found somewhere in the textbook on pages 284-285.)






      Please turn over for more problems

      Continued from other side

  2. On p. 288, there are four practice problems—the answers to those problems can be found on p. 417 in your textbook.

    The following problems should be done in a similar fashion—of course, the answers are NOT in your book—you need to come up with those on your own answers!

    Remember that if p is a pointer, its meaning depends on whether it is deferenced with a *.
    Also, how you think about it depends on whether it is on the right hand side (rhs) of an assignment statment (rvalue) or the left hand side (lhs) of an assignment statment (lvalue)


    1. (5 pts)

      int a=3, b=4, *ptr;

      ptr = &a;

       

    2. (5 pts)

      int a=5, b=6, *ptr=&a;

      b = *ptr;

       

    3. (5 pts)

      int a=7, b=8, c=9, *ptr=&b;

      a = *ptr;
      *ptr = c;

       

    4. (5 pts)

      int a=10, b=11, c=12 *p1=&b, *p2;

      p2 = &c; a = *p1; p1 = &a;

    5. (5 pts)

      int a=13, b=14, *p1=&a, *p2=&b, *p3;

      (*p1) = (*p2); p3 = p1;

       

    6. (5 pts)

      int a=15, b=16, *p1=&a, *p2=&b, *p3;

      p3 = p1;
      p1 = p2;
      p2 = p3;

       


End of H11