summaryrefslogtreecommitdiff
path: root/C/Pointers/addr.c
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /C/Pointers/addr.c
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'C/Pointers/addr.c')
-rw-r--r--C/Pointers/addr.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/C/Pointers/addr.c b/C/Pointers/addr.c
new file mode 100644
index 0000000..f77a9c6
--- /dev/null
+++ b/C/Pointers/addr.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+
+int main (void)
+{
+ /*
+ * Arrays in C are just sequential data items
+ * stored in a memory location
+ * The address of the array, is the same as the first
+ * element in the array
+ * The name of the array is also the address of the array.
+ */
+
+ char str1[] = "Hello cruel world"; /* \0 is automatically added */
+
+ /* Those variables look the same, but they are not. */
+
+ /* This is an array */
+
+ /*
+ * str2, is the address of the "ARRAY" which the characters
+ * of the string are stored
+ * str2 is not a 'pointer' to the location of the Hello char array
+ * str2 IS THE LOCATION of the array.
+ */
+ char str2[] = "Hello";
+
+ /*
+ * This is a pointer.
+ *
+ * Whose value is not the string, but the - address of that string -
+ *
+ */
+ char *str3 = "Goodbye";
+
+ str3 = NULL;
+ str2 = NULL;
+ /*
+ * &str1, &str1[0] and str1, all points to the very same address.
+ *
+ * The array name, str1, is also the address of the array.
+ * */
+ printf("%s, %c, %d, %d %d\n", str1, str1[0], &str1, &str1[0], str1);
+
+ printf("%p %p %s\n", &str2, str2, str2);
+ printf("%p %p %s\n", &str3, str3, str3);
+
+}