Introduction to C, Homework 3

Due Wednesday, 5 November 2008 by 11:59 PM


Question 1

Are arrays passed by value or reference? Are array elements passed by value or reference?


Question 2

Can items that, by default, are passed by value in C be passed by reference? Conversely, can items that are passed by reference also be passed by value?


Question 3

Reduce the diamond program from HW 2 to the following:

#include <stdio.h>

#include “diamond.h”

int main( void )
{
   int user_input;
   
   printf( "Enter the size of your diamond: " );
   scanf( "%d", &user_input );

   draw_diamond( &user_input );

   return 0;
}
   

You must implement the following functions

void draw_diamond( int *input );
void print_odd( int *number );
void print_even( int *number );

in an implementation file (diamond.c) and the prototypes for those functions in a header file (diamond.h).

Make extensive use of pointers and the principle of least privilege.