Table of Contents

Project: More Pointer Fun!!!

A project for CSCS 1320 by Paul Sechrist during the Spring 2013.

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

Code

1
#include <stdio.h>
#include <stdlib.h>
 
#define NUM_SCORES 5
#define SCORE0 87
#define SCORE1 92
#define SCORE2 97
#define SCORE3 83
#define SCORE4 79
 
int main()
{
        unsigned char *scores;
        float average;
 
        scores = (unsigned char *) malloc (sizeof(unsigned char) * NUM_SCORES);
        *(scores+0) = SCORE0;
        *(scores+1) = SCORE1;
        *(scores+2) = SCORE2;
        *(scores+3) = SCORE3;
        *(scores+4) = SCORE4;
 
        average = ((float)(SCORE0+SCORE1+SCORE2+SCORE3+SCORE4)/NUM_SCORES);
        fprintf(stdout, "Your scores are, %hhu, %hhu, %hhu, %hhu, %hhu\n", SCORE0, SCORE1, SCORE2, SCORE3, SCORE4);
        fprintf(stdout, "Your average is, %f\n", average);
 
        return(0);
}

Execution

lab46:~/src$ ./pointerarrays
Your scores are, 87, 92, 97, 83, 79
Your average is, 87.599998
lab46:~/src$ 
lab46:~/src$ gcc -o gettinginput1 gettinginput1.c
gettinginput1.c:28: warning: passing argument 1 of 'fprintf' from incompatible pointer type
/usr/include/stdio.h:333: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
gettinginput1.c:28: warning: passing argument 2 of 'fprintf' makes pointer from integer without a cast
/usr/include/stdio.h:333: note: expected 'const char * __restrict__' but argument is of type 'int'
gettinginput1.c:29: warning: passing argument 1 of 'fprintf' from incompatible pointer type
/usr/include/stdio.h:333: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
gettinginput1.c:29: warning: passing argument 2 of 'fprintf' makes pointer from integer without a cast
/usr/include/stdio.h:333: note: expected 'const char * __restrict__' but argument is of type 'short int'
gettinginput1.c:30: warning: passing argument 1 of 'fprintf' from incompatible pointer type
/usr/include/stdio.h:333: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
gettinginput1.c:30: warning: passing argument 2 of 'fprintf' makes pointer from integer without a cast
/usr/include/stdio.h:333: note: expected 'const char * __restrict__' but argument is of type 'char'
gettinginput1.c:31: warning: passing argument 1 of 'fprintf' from incompatible pointer type
/usr/include/stdio.h:333: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
gettinginput1.c:31: error: incompatible type for argument 2 of 'fprintf'
/usr/include/stdio.h:333: note: expected 'const char * __restrict__' but argument is of type 'float'
lab46:~/src$ ./gettinginput1
Enter an integer: 1
Enter another integer: 2
Enter a short int: 3
Enter a char: Enter a float value: 4
Your ints are: 1 and 2
Your short int is: 3
Your char is: '
'
Your float (trimmed at 2 decimal places) is: 4.00
lab46:~/src$