9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit ba014b5e794406752b6258dede5be34e91260a60
parent fcc549fe5221ca170ef446834d5a411530ce55b2
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Thu, 27 May 2021 11:46:57 -0300

Update: updated guia03/ex07.c & added guia04/ex08.c

Diffstat:
Aguia04/ex07.c | 17+++++++++++++++++
Aguia04/ex08.c | 29+++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/guia04/ex07.c b/guia04/ex07.c
@@ -0,0 +1,17 @@
+/*
+ * What is the difference between NULL and '\0'?
+ * The difference is that NULL is defined as '\0' casted to void*,
+ * they both are evaluated to the constant value 0, 
+ * but they are used in different contexts, in general NULL
+ * is used to check for pointers, and '\0' for the ends of a string.
+*/
+
+#include <stdio.h>
+
+int main (void)
+{
+    printf("'\\0' = %d", '\0');
+    printf("'NULL' = %d", NULL);
+
+    return 0;
+}
diff --git a/guia04/ex08.c b/guia04/ex08.c
@@ -0,0 +1,29 @@
+/*    G04E08 - Different declarations of arrays and pointers
+ *    by Martin J. Kloeckner    
+ *    github.com/klewer-martin    */
+
+#include <stdio.h>
+#include <string.h>
+
+int main (void)
+{
+    char cadena1[] = "Hola"; 
+    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; */
+
+    /* The first of this statements doesn't work because cadena1 is an array so the adress can't be modified; cadena2 instead is a pointer, so we are just pointing cadena2 to "Chau". */
+    cadena1 = "Chau";
+    cadena2 = "Chau"; 
+    
+    /* The first statement works because, it's how strcpy is intended to be used, the third statement its wrong because it's trying to copy more characters than cadena1 it's meant to hold, and the second statement won't work because strcpy treats the first argument as an array, and cadena2 ain't an array  */ 
+    strcpy(cadena1,"Chau"); 
+    strcpy(cadena2,"Chau"); 
+    strcpy(cadena1,"Hola y chau"); 
+
+    return 0;
+}