UNB/ CS/ David Bremner/ tags/ union

This feed contains pages with tag "union".

Preprocessor

  1. Given the following definition

    #define DOUBLE(X) 2*X

    • What is the value of DOUBLE(1+2)
    • What is the value of 4/DOUBLE(2)
    • Fix the definition of DOUBLE, and test it.
  2. Write a macro DISP(f,x) so that

    DISP(sqrt,3.0)

expands into

 printf("sqrt(%g) = %g\n", 3.0, sqrt(3.0));

Test your macro.

Structures and Unions

Given the following definitions

struct point { int x, y };
struct shape {
       enum { RECTANGLE, CIRCLE } shape_kind;
       struct point center;
       union {
         struct {
           int height, width;
         } rectangle;
         struct {
           int radius;
         } radius;
       } u;
}

Correct any of the following statements that are illegal:

  1. s.shape_kind = RECTANGLE;

  2. s.center.x = 10 ;

  3. s.height = 25;

  4. s.u.rectangle.width = 8;

  5. s.u.circle=5;

  6. s.u.radius=6;

Function pointers

Write the function sum with prototype

int sum(int (*f)(int), int start, int end)

The call sum(g,i,j) should return g(i)+g(i+1)+...+g(j)

Posted Mon 30 Nov 2009 12:19:00 PM Tags: /tags/union
  • Chapter 16 Structures, Unions

  • slides printable

  • about slides

Posted Wed 18 Nov 2009 12:00:00 AM Tags: /tags/union