From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONCEPT/string.html | 157 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 reference/C/CONCEPT/string.html (limited to 'reference/C/CONCEPT/string.html') diff --git a/reference/C/CONCEPT/string.html b/reference/C/CONCEPT/string.html new file mode 100644 index 0000000..532f9d7 --- /dev/null +++ b/reference/C/CONCEPT/string.html @@ -0,0 +1,157 @@ +Strings + +
+

Strings.

+
+

+

Pointers to strings.

+ +C does not have a "string" datatype. +To create a string you have to use a char array +or a char pointer. +If you are not familur with char arrays +I recomend that you read about them now. +

+To recap, a char pointer is +defined like this: +

+ + +
+
+ 
+     main()
+     {
+       char *Text;
+     }
+     
+
+
+

+ +All this program does is reserve storage that will hold an address. +At this point the address could be anything. To initalize Text +you can code: +

+ +
+
+    
+     main()
+     {
+       char *Text = "Thunder";
+     }
+     
+
+ + +Text now has the address of the first character in Thunder. +Graphically, things look like this. + +

+
+      (Address) (Data)
+      
+           ---- ----    
+          | F1 | 00 <------- Text   
+	  |----|----|               (Data) (Adress)
+          | F2 | 00 |                 -------------
+	  |----|----|         -------> 54 (T) | D1 |
+	  | F3 | 00 |        |       |--------|----|
+	  |----|----|  *Text |       | 68 (h) | D2 |
+	  | F4 | D1 | -------        |--------|----|
+	   ---------                 | 75 (u) | D3 |
+	                             |--------|----|
+				     | 6E (n) | D4 |
+				     |--------|----|
+                                     | 64 (d) | D5 |
+                                     |--------|----|
+                                     | 65 (e) | D6 |
+                                     |--------|----|
+                                     | 72 (r) | D7 |
+                                     |--------|----|     
+                                     | 00     | D8 |
+                                      -------------
+
+
+

+ +Please note the 00 at the end of Thunder. This is +the NULL character and is used to mark the +end of a string. +

+If we wanted to O/P the data pointed to by a char pointer + we can code. +

+ + +
+Source +
+
+
+     main()
+     {
+       char *Text1 = "Thunder";          /* Define and initalize */
+       char *Text2;                      /* Define only          */
+       
+       Text2 = "Bird";                   /* Point to some text   */
+       
+       printf("%s%s\n", Text1, Text2);
+     }
+     
+
+
+Result +
+
+
+      ThunderBird
+
+
+
+

+ +This is all very well, but there is a MAJOR problem! Thunder +and Bird are +constants, they cannot be +changed in anyway. +We need a method of pointing to some storage that can be altered +and true to form, C provides a function called +malloc to do just that. +

+ +


+

See Also:

+ + +VOID keyword. +
+

+

+ + + + +
+Top + +Master Index + +Keywords + +Functions +
+
+

+ +


+
Martin Leslie + +
+ + -- cgit v1.2.3-54-g00ecf