CSC373/406: Example: Passing pointers as parameters [8/8] Previous pageContents

file:square4.c [source]
00001: #include <stdio.h>
00002: // prototypes
00003: void square(int *numb);
00004: int main()
00005: {
00006:   int val = 3;
00007: 
00008:   square(&val);
00009: 
00010:   printf("val = %d\n", val); // prints 4, since val was changed in square
00011: 
00012:   return 0;
00013: }
00014: 
00015: void square(int *numb)
00016: {
00017:   int sq;
00018: 
00019:   (*numb)++;            // '*numb' (same as 'val' in main) MODIFIED HERE to 4
00020:   sq = (*numb) * (*numb);
00021:   printf("sq = %d\n", sq); // prints 16
00022: }
00023: 

Previous pageContents