commit d0c19ae0630f78d68f1fc304804a7ef5a336ca7d
parent d5f64b53a0189299208f7107650b762f34b19163
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 1 Jun 2021 18:02:44 -0300
Update: ex08.c and added ex09.c
Diffstat:
2 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/guia04/ex08.c b/guia04/ex08.c
@@ -11,7 +11,6 @@ int main (void)
char *cadena2 = "Hola";
/* The difference is that cadena1 is an array that contains the secuence of chars "Hola", and cadena2 is a pointer which points to an array of chars that contains "Hola"; */
-
char meses1 [12][] = {"Enero", "Febrero", ... , "Diciembre"};
char * meses2 [12] = {"Enero", "Febrero", ... , "Diciembre"};
/* Similar as the previous example, meses1 is an array of arrays of chars, and meses2 is a pointer to an array of chars which the maximum length is 12; */
diff --git a/guia04/ex09.c b/guia04/ex09.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <float.h>
+
+#define BUFFER_LEN 10
+#define MAX_IN_LEN 6
+
+int main (void)
+{
+ char aux, buffer[BUFFER_LEN + 1];
+ float *pf[10];
+ double *sum;
+ size_t i;
+
+ printf("%d\n", FLT_MAX);
+ printf("%d\n", FLT_DIG);
+
+ if(!(sum = (double *)calloc(1, sizeof(double))))
+ return 1;
+
+ /* Makes every pointer point to a space in memmory with space for one float number */
+ for(i = 0; i < 10; i++) {
+ if(!(pf[i] = (float *)calloc(1, sizeof(float)))) {
+ for(size_t j = i; j > 0; j--)
+ free(pf[j]);
+
+ return 1;
+ }
+ }
+
+ /* Populates the array with user input converted to float, computes the sume of all
+ * the converted numbers and then frees the memmory since it's not longer needed */
+ for(i = *sum = 0; i < 10 ; i++) {
+ if(!fgets(buffer, MAX_IN_LEN, stdin)) return 2;
+ *sum += (*pf[i] = strtof(buffer, NULL));
+
+ printf("%p: %5f\n", pf[i], *pf[i]);
+ free(pf[i]);
+ while(((aux = getchar()) != 10) && (aux != 13))
+ putchar(aux);
+ }
+
+ printf("\nsum: %5f\n", *sum);
+
+ free(sum);
+ return 0;
+}