Created
January 15, 2015 05:06
-
-
Save pablq/30a1662893fdd0e4b124 to your computer and use it in GitHub Desktop.
just brushing up a bit. haven't looked at C code in a while.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define INT_MAX 1000 | |
#define UINT_MAX 10 | |
char* getString(void); | |
int getInt(void); | |
int* getAge(void); | |
int main (int argc, char* argv[]) | |
{ | |
int n; | |
if (argv[1] == NULL) | |
{ | |
printf("proper use: ./pointer_practice <number of dolphins>\n"); | |
return 1; | |
} | |
else | |
{ | |
n = atoi(argv[1]); | |
} | |
int* dolphins[n]; | |
for (int i = 0; i < n; i ++) | |
{ | |
dolphins[i] = getAge(); | |
} | |
int oldest; | |
if (n > 0) | |
{ | |
oldest = *dolphins[0]; | |
} | |
else | |
{ | |
oldest = 0; | |
} | |
for (int i = 0; i < n; i++) | |
{ | |
if(*dolphins[i] > oldest) | |
{ | |
oldest = *dolphins[i]; | |
} | |
} | |
printf("The oldest dolphin is %d!\n", oldest); | |
} | |
char* getString(void) | |
{ | |
// growable buffer for chars | |
char* buffer = NULL; | |
// capacity of buffer | |
unsigned int capacity = 0; | |
// number of chars actually in buffer | |
unsigned int n = 0; | |
// declace placeholder c | |
char c; | |
// iteratively get chars from standard input | |
while ((c = fgetc(stdin)) != '\n' && (c != EOF)) | |
{ | |
// grow buffer if necessary | |
if (n + 1 > capacity) | |
{ | |
// determine new capacity; start at 32 then double | |
if (capacity == 0) | |
{ | |
capacity = 32; | |
} | |
else if (capacity <= (UINT_MAX / 2)) | |
{ | |
capacity *= 2; | |
} | |
else | |
{ | |
free(buffer); | |
return NULL; | |
} | |
// extend buffer's capacity | |
char* temp = realloc(buffer, capacity * sizeof(char)); | |
if (temp == NULL) | |
{ | |
free(buffer); | |
return NULL; | |
} | |
buffer = temp; | |
} | |
// append current character to buffer | |
buffer[n++] = c; | |
} | |
// return NULL if user provided no input | |
if (n == 0 && c == EOF) | |
{ | |
return NULL; | |
} | |
// minimize buffer | |
char* minimal = malloc((n + 1) * sizeof(char)); | |
strncpy(minimal, buffer, n); | |
free(buffer); | |
// terminate string | |
minimal[n] = '\0'; | |
// return string | |
return minimal; | |
} | |
int getInt(void) | |
{ | |
// try to get an int from user | |
while (1) | |
{ | |
// get line of text, returning INT_MAX on failure | |
char* line = getString(); | |
if (line == NULL) | |
{ | |
return INT_MAX; | |
} | |
// return an int if only an int (possibly with | |
// leading and/or trailing whitespace was provided | |
int n; | |
char c; | |
if (sscanf(line, " %d %c", &n, &c) == 1) | |
{ | |
free(line); | |
return n; | |
} | |
else | |
{ | |
free(line); | |
printf("Retry: "); | |
} | |
} | |
} | |
int* getAge(void) | |
{ | |
int* age = malloc(sizeof(int)); | |
do | |
{ | |
printf("How old is the dolphin (> 0)? "); | |
*age = getInt(); | |
} | |
while (*age < 1); | |
printf("This dolphin is %d\n", *age); | |
return age; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment