c-first-steps

a C playground
Index Commits Files Refs README
commit d718365f88bc92a3c94ed77a48cf7642acce253b
parent 3ff39be9579d357f1ece88dece3dd0e03eab7b7d
Author: Martin J. Klockner <martin.cachari@gmail.com>
Date:   Wed, 18 Nov 2020 00:11:07 -0300

Added pointer validation to function str_len;

Diffstat:
M95.11/guia03/ej8.c | 6+++++-
M95.11/guia03/ej8_modular.c | 5++++-
M95.11/guia03/ej9_modular.c | 6+++++-
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/95.11/guia03/ej8.c b/95.11/guia03/ej8.c
@@ -2,8 +2,12 @@
 #include <string.h>
 
 size_t str_len(const char *str) {
+    if(str == NULL)
+        return 1;
+
     size_t i;
-    for(i = 0; str[i] != '\0'; i++) {}
+    for(i = 0; str[i] != '\0'; i++)
+        ; 
 
     return i;
 }
diff --git a/95.11/guia03/ej8_modular.c b/95.11/guia03/ej8_modular.c
@@ -1,8 +1,11 @@
 #include <stdio.h>
 
 size_t str_len(const char *str) {
+    if(str == NULL)
+        return 1;
     size_t i;
-    for(i = 0; str[i] != '\0'; i++) {}
+    for(i = 0; str[i] != '\0'; i++) 
+        ;
 
     return i;
 }
diff --git a/95.11/guia03/ej9_modular.c b/95.11/guia03/ej9_modular.c
@@ -1,8 +1,12 @@
 #include <stdio.h>
 
 size_t str_len(const char *str) {
+    if(str == NULL)
+        return 1;
+
     size_t i;
-    for(i = 0; str[i] != '\0'; i++) {}
+    for(i = 0; str[i] != '\0'; i++) 
+        ;
 
     return i;
 }