1
+ /*******************************************************************************
2
+ *
3
+ * Program: Char Array String Vs. Poointer To String Literal
4
+ *
5
+ * Description: The difference between a string stored in a char array compared
6
+ * to a pointer to a string literal in C. i.e. the difference between
7
+ * char s[] = "string"; vs char *s = "string";
8
+ *
9
+ * YouTube Lesson: https://www.youtube.com/watch?v=Qp3WatLL_Hc
10
+ *
11
+ * Author: Kevin Browne @ https://portfoliocourses.com
12
+ *
13
+ *******************************************************************************/
14
+
15
+ #include <stdio.h>
16
+
17
+ int main ()
18
+ {
19
+ // String literals like "abcdef" exist in some place in memory, where
20
+ // exactly is not defined by the C standard. We can use string literals to
21
+ // initialize a char array to store a string, as we do here. The char array
22
+ // s1 will be 7 chars long (due to the extra null terminator), and it will
23
+ // be in a place in memory called the stack where we know it is possible
24
+ // to modify the values that are stored there.
25
+ char s1 [] = "abcdef" ;
26
+
27
+ // Change the first char of the char array s1
28
+ s1 [0 ] = 'X' ;
29
+
30
+ // Output the modified s1 string which should now be Xbcdef
31
+ printf ("s1: %s\n" , s1 );
32
+
33
+ // The string literal "abcdef" will exist *somewhere* in-memory during our
34
+ // program's execution (again we don't exactly know where as it is not
35
+ // defined by the C standard and is up to the compiler). And s2 is a
36
+ // pointer to the first char of this string literal (which is terminated
37
+ // with a null terminator character).
38
+ const char * s2 = "abcdef" ;
39
+
40
+ // Because we've made s2 a pointer to a const char above, the compiler will
41
+ // flag an attempt (like below) to modify what s2 is pointing to as an error.
42
+ // If we did not user the const keyword when declaring s2, the compiler
43
+ // would allow the below statement to be compiled without error. But we
44
+ // will almost certainly get a runtime error as a result because the
45
+ // modification of a string literal is "undefined" by the C standard. This
46
+ // does not strictly speaking mean it is not allowed, but as a practical
47
+ // matter the compiler will not support this and we will get a runtime
48
+ // error related to memory acces. As a result, if we're going to have a
49
+ // pointer to a string literal, we should make sure it's a const char
50
+ // pointer so that we don't accidentally attempt to modify the string.
51
+ // s2[0] = 'X';
52
+
53
+ // The char array s1 is like a constant pointer, so we can't increment
54
+ // s1 to have it point to the next element in the array.
55
+ // s1++;
56
+
57
+ // s2 is a non-const pointer, so we actually CAN increment s2 to have it
58
+ // point to the next char in the string literal. Note that s2 is a pointer
59
+ // to a const char, NOT a 'const pointer', there is a difference.
60
+ s2 ++ ;
61
+
62
+ // Output s2 and we'll get bcdef as s2 has been set to point to the next
63
+ // char in the string literal (i.e. the char 'b' onwards)
64
+ printf ("s2: %s\n" , s2 );
65
+
66
+ // We cannot assign a string to s1, we would need to use something like
67
+ // strcpy() in the string.h library to change the string stored in s1
68
+ // s1 = "new string";
69
+
70
+ // We actually CAN assign a string literal to s2 because what will happen
71
+ // is that "new string" will be a new string literal in memory, and s2
72
+ // will be changed to point to THIS string literal now.
73
+ s2 = "new string" ;
74
+
75
+ // If we output s2 now we'll get "new string"
76
+ printf ("s2: %s\n" , s2 );
77
+
78
+ // If we use the sizeof operator with s1 and s2, we'll get 7 bytes for s1
79
+ // because it stores 7 chars of 1 byte each. These 7 chars are stored on
80
+ // the stack. But with s2 we'll get 8 bytes because s2 is REALLY just a
81
+ // pointer on the stack and sizeof will give us the size of that pointer
82
+ // NOT the size of the string literal that it points to.
83
+ printf ("sizeof(s1): %d\n" , sizeof (s1 ));
84
+ printf ("sizeof(s2): %d\n" , sizeof (s2 ));
85
+
86
+ return 0 ;
87
+ }
0 commit comments