/* p24.c */ /* This program illustrates the use of pointers in */ /* reading and processing an array of integers */ /* An upper bound on the number of ints to be read */ /* must be specified on the command line.. */ /* p2 1000 */ /* This program is actually broken.. use gdb to find */ /* and fix the bugs. */ #include main( int argc, /* number of command line args */ char* argv[]) /* array of pointers to args */ { int* base; // base address of the array int* loc; // array "index" int max; // max number of ints to be read int count; // actual number of ints read int largest; // largest int int i; if (argc < 2) /* Make sure at least one arg was given */ { printf("Usage is p20 upper-bound \n"); exit(1); } /* The pointer argv[0] points to the name of the program (p20) */ /* argv[1] points to the first command line argument */ /* The atoi() function converts the ascii character rep of */ /* an integer to a binary int value. */ max = atoi(argv[1]); if (max <= 0) { printf("upper-bound must be a positive integer \n"); exit(2); } count = 0; base = (int *)malloc(4 * max); loc = base; /* Read in the integers from standard input making sure */ /* not to overrun the size of the array */ while (scanf("%d", loc) == 1) { loc = loc + 1; count = count + 1; if (count == max) break; } loc = base; largest = *loc; loc = loc + 1; for (i = i; i < count; i++) { if (*loc > largest) *loc = largest; loc = loc + 1; } printf("Largest was %d \n", largest); }