Week 6 Tutorial — Structured Data Types & Pointers

    1. Consider the declaration of the variable s1:

      struct {
         char c;
         int  i;
         char d;
         int  j;
         char e;
      } s1;
      

      Assuming the address of the first available memory location is 0xdeadbee0, show the memory locations of each of the variables in s1.

      1. How many bytes are needed to store s1?
      2. How much of this space is just 'padding'?
      3. What is the first available memory location after s1?
    2. Consider the declaration of variable s2:

      struct {
         int  i;
         int  j;
         char c;
         char d;
         char e;
      } s2;
      

      Again assuming the address of the first available memory location is 0xdeadbee0, show the memory locations of each of the variables in s2. (Notice that s1 and s2 contain the same variables but in a different order.)

      1. How many bytes are needed to store s2?
      2. How much of this space is just 'padding'?
      3. What is the first available memory location after s2?
  1. A philatelic society has members whose personal details needs to be computerised. A member has a name, a phone number and an address where an address consists of a house number, a street name and a city. The fields name, street and city are strings; the fields phone and house are integers. Write C data structures to represent this data, preserving the hierarchy in the data.

  2. (Pointer Refresher)
    1. Given the definitions

      int x;
      int *p;
      int **q;
      

      what is the type of each of the following expressions?

      x
      *x
      &x
      p
      *p
      &p
      q
      *q
      &q

    2. What is an equivalent but simpler expression that always produces the same result as:

      *&x 

      for any variable x?

    3. Given the following definition:

      int data[12] = {5, 3, 6, 2, 7, 4, 9, 1, 8};
      

      and assuming that &data[0] == 0x10000, what are the values of the following expressions?

      data + 4
      *data + 4
      *(data + 4)
      data[4]
      *(data + *(data + 3))
      data[data[2]]

  3. Consider the following piece of code:

    typedef struct {
       char name[20];
       int age;
       char gender;
       char birthday[10];
    } PersonT;
    
    PersonT per1;
    PersonT per2;
    PersonT *ptr;
    
    ptr = &per1;
    strcpy(per1.name, "Jack");
    ptr->gender = 'M';
    ptr = &per2;
    strcpy(ptr->name, "Jill");
    ptr->gender = 'F';
    per1.age = 20;
    per2.age = 19;
    ptr = &per1;
    strcpy(per2.birthday, "07-06-1990");
    strcpy(ptr->birthday, "02-02-1990");
    

    What are the values of the fields in the per1 and per2 record after execution of the above statements?