UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

5.11.  Pointer Exercises

The following problems require you to know the operands to manipulate pointers, how are the data allocated in memory and the concept of indirection. We recommend also that you write all the data type definitions, declarations and code fragments in a text file in your development environment and check its results by compiling and executing. Furthermore, for the exercises the following basic data type sizes are assumed:

Type Size (bytes)
char, unsigned char 1
short int, unsigned short int 2
int, unsigned int, long int, unsigned long int 4
float4
double, long double 8
Pointer of any type 4

Suppose that the following data structure are defined to store information about the cells with which a mobile phone can connect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define SIZE 100
/* Information about the cell */
struct cell_information 
{
    char name[SIZE];                 /* Cell name */
    unsigned int identifier;        /* Cell identifier */
    float signal_quality;            /* Signal Quality (between 0 and 100) */
    struct information_carrier *carrier_ptr; /* Pointer to a second structure */
};

/* Information about the carrier */
struct information_carrier 
{
    char name[SIZE];           /* String with the carrier name */
    unsigned int priority;    /* Priority of the connection */
    unsigned int last_checked; /* Last time it was checked*/ 
};

Answer the following questions:

  1. What is the size in bytes of a data structure of type struct cell_information?

  2. The following two lines declare two variables. Which of them occupies more space in memory?

    struct cell_information a;
    struct cell_information *b;
  3. What is the size of the following variables?

    struct cell_information *ptr1, *ptr2;
    struct information_carrier *i1, *i2;
  4. If a variable of type struct cell_information is stored in memory starting at position 100, what is the address of each of its fields?

  5. If a variable of type struct cell_information * is stored in the memory location 100, what is the address of each of its fields?

  6. Which changes would you do in the definitions on the left to make them equivalent to the descriptions on the right?

    struct cell_information c;
    struct cell_information **c_ptr;
    // a cell_information structure
    // a pointer to a cell_information structure
  7. Can the following assignments be made?

    1
    2
    3
    4
    struct cell_information c;
    struct cell_information *c_ptr = &c;
    struct cell_information d;
    struct cell_information *d_ptr = c_ptr;
  8. Consider the following declaration and assignment:

    1
    2
    3
    4
    struct cell_information c;
    struct cell_information *c_ptr;
    
    c_ptr = *c;

    Is it correct? And if so, What is the content of variable c_ptr (we are not asking for what is pointing to, but its content)?

  9. If a variable is declared as struct cell_information c;, what data type is returned by the expression &c.carrier_ptr?

  10. Given the following code:

    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
    struct pack3 
    { 
        int a;
    };
    struct pack2 
    {
        int b;
        struct pack3 *next;
    };
    struct pack1 
    {
        int c;
        struct pack2 *next;
    };
    
    struct pack1 data1, *data_ptr;
    struct pack2 data2;
    struct pack3 data3;
    
    data1.c = 30;
    data2.b = 20;
    data3.a = 10;
    dataPtr = &data1;
    data1.next = &data2;
    data2.next = &data3;

    Decide which of the following expressions are correct, and if so, which value is being accessed.

    Expression Correct Value
    data1.c
    data_ptr->c
    data_ptr.c
    data1.next->b
    data_ptr->next->b
    data_ptr.next.b
    data_ptr->next.b
    (*(data_ptr->next)).b
    data1.next->next->a
    data_ptr->next->next.a
    data_ptr->next->next->a
    data_ptr->next->a
    data_ptr->next->next->b
  11. Suppose that a program contains the following declarations and assignments:

    1
    2
    3
    4
    cell_info c;
    cell_info_ptr c_ptr = &c;
    info_carrier carrier;
    info_carrier_ptr car_ptr = &ca;

    The c structure contains the field carrier_ptr precisely to store the information about a carrier. Which expression do you need to write in the code to store the information in the structure carrier as part of the structure c? Taking into account the values assigned in the declarations, write four equivalent expressions (use c, c_ptr, carrier and car_ptr).

  12. Suppose that the application in which these definitions are used requires to store the information of up to 10 cells. Which data structure would you define?

  13. Write a loop with the variable declared in the previous exercise that assigns the empty value to the carrier_ptr field.

  14. The information about the cells stored in the data structure of the previous exercise is used by an application to remember which one of them is the closet one. This information may change through time. Which data type do you propose to store this information? Offer two alternatives.

  15. A data structure of type cell_info c is available and in its field carrier_ptr has a pointer to a structure info_carrier cr. What is the size of the structure c? What is the total amount of memory occupied including the information about the carrier?

  16. Write the body of the following function:

    void fill_in(cell_info_ptr data, unsigned int id, float sq, info_carrier_ptr carr_ptr)

    that assigns the values of the parameters id, sq and carr_ptr to the fields identifier, signal_quality and carrier_ptr respectively of the data structure pointed by the parameter data.

    How do you explain that this function assigns values to fields but does not return any result?

  17. Consider the two versions of the following program:

    Version 1 Version 2
    #include <stdio.h>
    struct package 
    { 
        int q; 
    };
    
    void set_value(struct package data, int value) 
    {
        data.q = value;
    }
    
    int main() 
    {
        struct package p;
        p.q = 10;
        set_value(p, 20);
        printf("Value = %d\n", p.q);
        return 0;
    }
    #include <stdio.h>
    struct package 
    { 
        int q; 
    };
    
    void set_value(struct package *d_ptr, int value) 
    {
        d_ptr->q = value;
    }
    
    int main() 
    {
        struct package p;
        p.q = 10;
        set_value(&p, 20);
        printf("Value = %d\n", p.q);
        return 0;
    }

    Version 1 of the program prints the value 10 on the screen, and version 2 prints the value 20. Explain why.

    Suggestion

    You may copy and paste the code in a file in your development environment and verify that the two programs behave as stated.

  18. How much memory occupy these two structures? What is the difference among them?

    info_celda t[SIZE];
    cell_info *t[SIZE];
  19. An application to manage pictures in your mobile phone has defined the catalog of pictures in the following way:

    #define SIZE_NAME
          
    struct picture_info 
    {
        char name[SIZE_NAME];
        int date_time;
    } pictures[SIZE];

    What is the size of this data structure? The application needs to create a second table of equal number of elements but instead of having the data structures, it has pointers to the picture data. In other words, it is a table with identical number of elements than pictures, but the elements are not data structures but their corresponding pointers. Write the declaration and the code to fill in this table.

  20. The following data definition is available

    #define SIZE 4
    struct coordinates 
    {
        int latitude;
        int longitude;
    } places[SIZE];
    
    places[0].latitude  = 200;
    places[0].longitude = 300;
    places[1].latitude  = 400;
    places[1].longitude = 100;
    places[2].latitude  = 100;
    places[2].longitude = 400;
    places[3].latitude  = 300;
    places[3].longitude = 200;

    The table stores four points obtained from the GPS in your mobile phone, each of them with its latitude and longitude stored as integers. There is an application that has obtained these points in this order but it needs to access this data in three different ways. The first one in the order in which they were obtained, and therefore, as they are stored in the table. The second is sorted by increasing latitude, and the third is sorted by increasing longitude. A first solution could be to reorder the table elements each time a new order is requested (for example, a request for the data sorted by latitude is received, but the previous one was sorted by longitude, thus the table is reordered). But it is very inefficient. Why? Would you be able to provide a more efficient solution that avoid continuously reordering the data? (Perhaps using pointers)