commit 57971e1f26b6cf7726bd4545a47a26ccf3932034
parent c2a44f60fd18618114ec9f94591e095d381aae15
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date: Thu, 17 Dec 2020 01:36:19 -0300
Added guia03/ex32.c;
Added guia08/malloc_basics.c which is the source code of a basic program
using malloc to allocate memmory in heap, also added guia08/ex01.c which
is the sc of a more complex program;
Diffstat:
3 files changed, 119 insertions(+), 19 deletions(-)
diff --git a/95.11/guia03/ex32.c b/95.11/guia03/ex32.c
@@ -0,0 +1,65 @@
+// Calculates the max or min in an array of numbers;
+// of type int;
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#define MAX_ELEM 10
+#define NO_ARGUMENT_ENTERED 1
+#define EXTREMO_ARGUMENT 1
+
+typedef enum {
+ MAXIMUM,
+ MINIMUM
+} extreme_t;
+
+typedef enum {
+ OK,
+ ERROR_INVOCATING_PROGRAM,
+ ERROR_NULL_POINTER
+} status_t;
+
+status_t validate_arguments (int argc, char ** argv);
+
+int main (int argc, char * argv[])
+{
+ size_t i;
+ int vector[MAX_ELEM] = {9, 10, 25, 31, 9, 5, 11, 46, 50, 20};
+ extreme_t extremo;
+ status_t st;
+
+ if((st = validate_arguments(argc, argv)) != OK)
+ return st;
+
+ extremo = atoi(argv[EXTREMO_ARGUMENT]);
+ if(extremo == MAXIMUM) {
+ // Find maximum element in array;
+ int maximum;
+ for(i = 0, maximum = INT_MIN; i < (sizeof(vector)/sizeof(vector[0])); i++)
+ if(vector[i] > maximum)
+ maximum = vector[i];
+
+ printf("%d\n", maximum);
+ } else if (extremo == MINIMUM){
+ // Find minimum element in array;
+ int minimum;
+ for(i = 0, minimum = INT_MAX; i < (sizeof(vector)/sizeof(vector[0])); i++)
+ if(vector[i] < minimum)
+ minimum = vector[i];
+
+ printf("%d\n", minimum);
+ }
+ return 0;
+}
+
+status_t validate_arguments (int argc, char ** argv) {
+ if(argc == NO_ARGUMENT_ENTERED)
+ return ERROR_INVOCATING_PROGRAM;
+
+ else if (argv == NULL)
+ return ERROR_NULL_POINTER;
+
+ else return OK;
+}
+
diff --git a/95.11/guia08/ex01.c b/95.11/guia08/ex01.c
@@ -1,44 +1,57 @@
+// Clones a given string entered as arguments of function main;
#include <stdio.h>
#include <stdlib.h>
-#include <ctype.h>
+#include <string.h>
-#define NO_ARG 1
-#define MAX_ARR_SIZE 100
+#define NO_ARGUMENT 1
+#define INIT_SIZE 100
typedef enum {
OK,
ERROR_PROGRAM_INVOCATION,
ERROR_NULL_POINTER,
- ERROR_ALLOC
+ ERROR_ALLOC_MEMORY
} status_t;
status_t validar_argumentos(int argc, char **argv);
int main (int argc, char * argv[]) {
- char *p;
- int i;
+ char *dest;
+ size_t i;
status_t st;
+
+// Verify if arguments are right;
if((st = validar_argumentos(argc, argv) != OK))
return st;
- if((p = (char *)malloc(2 * (argc * MAX_ARR_SIZE))) == NULL)
- return ERROR_ALLOC;
-
- for(i = 0; i < (argc - 1); i++, argv++)
- strlen((char *)argv);
- (char *)p = ((char *)(*(++argv)));
-
-
- free(p);
- return 0;
+// Allocates memmory in heap of size INIT_SIZE;
+ if((dest = (char *)malloc(INIT_SIZE * sizeof(char))) == NULL)
+ return ERROR_ALLOC_MEMORY;
+
+// Assigns 1 to i to avoid the first element of argv,
+// which is the program name. Then copies every string of argv
+// into dest and prints it on stdout;
+ for(i = 1; (int)i < argc; i++) {
+ strcpy(dest, argv[i]);
+ // Puts a space in between strings, avoiding a blank
+ // space after first string is printed;
+ if(i != 1) putchar(' ');
+ printf("%s", dest);
+ }
+
+// Adds the new line character;
+ putchar('\n');
+ free(dest);
+ return OK;
}
status_t validar_argumentos(int argc, char **argv) {
- if(argc == NO_ARG)
+ if(argc == NO_ARGUMENT)
return ERROR_PROGRAM_INVOCATION;
+
else if(argv == NULL)
return ERROR_NULL_POINTER;
-
- return 0;
+
+ else return OK;
}
diff --git a/95.11/guia08/malloc_basics.c b/95.11/guia08/malloc_basics.c
@@ -0,0 +1,22 @@
+// Stores an array of numbers in heap, then prints it on
+// stdout finally frees the memory used;
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ARR_LEN 10
+
+int main (void)
+{
+ int *p;
+
+ if((p = (int*)malloc(ARR_LEN * sizeof(int))) == NULL)
+ return 1;
+
+ for(int i = 0; i < ARR_LEN; i++) {
+ *(p + i) = i;
+ printf("%d\n", *(p + i));
+ }
+ free(p);
+ return 0;
+}