CIS-4020 Homework #3: Selected C Topics

Due: Thursday, September 15, 2016

You may find some material in my C Tutorial useful for this assignment. Although not directly required for this assignment, you could start reading Chapter 3 in the text on processes to start preparing for our next topic.

This assignment asks you to practice with a few C features that you might not be very familiar with. Note that the code I'm asking you to write for this assignment is not "normal" code and is not necessarily how you would want to write regular programs!

  1. Let the phrase "byte rotate right" mean that the bytes of a 32 bit unsigned int are moved one position toward the least significant end, with the least significant byte being rotated to the most significant position. For example an integer such as 0x12345678 would become 0x78123456.

    Write a C function byte_rotate_array declared as follows:

            void byte_rotate_array( unsigned int *array, size_t element_count );
          

    The function is to do a "byte rotate right" of every array element. It should do this, however, by treating the array as an array of unsigned characters and moving the characters (bytes) around individually. Do not do any mathematical operations such as division or modulus.

  2. Consider the type: "pointer to function taking (pointer to function taking int and returning void) and returning (pointer to function taking pointer to char and long and returning pointer to char). Write the declaration of a variable named 'p' that has this type. Take the following intermediate steps:

    Don't use any parameter names in your declarations above. While it is legal (and maybe preferable) to do so, those names will clutter your answer too much for our purposes here.

    BONUS (1 pt): Write the declaration of 'p' without the help of the typedef names.

  3. Consider the following structure type:

            struct Example {
                char c;
                short s;
                int i;
            };
          

    Write a small program that prints out the offset into the structure for each of the members. Compare these offsets with the sizes of the members. Is the compiler using any padding between members and if so, how much? HINT: Declare an object of type struct Example and ask the compiler to give you the address of each member of that object. Convert the addresses into integers and find the differences between them. Do not use the standard offsetof macro!

    Note that the C standard requires the offset of the first member be zero (no padding before the first member). How can you check if your compiler is complying with that requirement?


Last Revised: 2016-09-06
© Copyright 2016 by Peter C. Chapin <PChapin@vtc.vsc.edu>