UNB/ CS/ David Bremner/ tags/ struct

This feed contains pages with tag "struct".

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 AST Tags: /tags/struct
  • Chapter 17 variable sized structs. Chapter 15, multiple compilation units, include files

  • slides printable

  • about slides

Posted Mon 30 Nov 2009 12:00:00 AM AST Tags: /tags/struct
  • Chapter 17 Dynamic memory allocation, linked lists, variable sized structs

  • slides printable

  • about slides

Posted Fri 27 Nov 2009 12:00:00 AM AST Tags: /tags/struct
  • Chapter 16 Structures, Unions

  • slides printable

  • about slides

Posted Wed 18 Nov 2009 12:00:00 AM AST Tags: /tags/struct
Posted Mon 16 Nov 2009 12:00:00 AM AST Tags: /tags/struct