1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
<title>Strings</title>
<body bgcolor="#ffffcc">
<hr>
<center><h1>Strings.</h1></center>
<hr>
<p>
<h2>Pointers to strings.</h2>
C does not have a "string" <a href=data_types.html>datatype</a>.
To create a string you have to use a <a href=arrays.html#char>char array</a>
or a <a href=data_types.html#char>char</a> pointer.
If you are not familur with <a href=arrays.html#char>char arrays</a>
I recomend that you read about them now.
<p>
To recap, a <a href=data_types.html#char>char</a> pointer is
<a href=../glossary.html#definitions>defined</A> like this:
<p>
<table border=2 width=100% bgcolor=ivory>
<tr><td>
<pre>
main()
{
char *Text;
}
</pre>
</td></tr>
</table>
<p>
All this program does is reserve storage that will hold an address.
At this point the address could be anything. To initalize <b>Text</b>
you can code:
<p>
<table border=2 width=100% bgcolor=ivory>
<tr><td>
<pre>
main()
{
char *Text = "Thunder";
}
</td></tr></table>
</pre>
<b>Text</b> now has the address of the first character in <b>Thunder</B>.
Graphically, things look like this.
<pre>
(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 |
-------------
</pre>
<p>
Please note the <b>00</b> at the end of <b>Thunder</b>. This is
the <a href=../SYNTAX/null.html>NULL</a> character and is used to mark the
end of a string.
<p>
If we wanted to O/P the data pointed to by a <b>char pointer</b>
we can code.
<p>
<table border=2 width=100% bgcolor=ivory>
<tr align=center><td>
<b>Source</b>
</td></tr><tr><td>
<pre>
main()
{
char *Text1 = "Thunder"; /* Define and initalize */
char *Text2; /* Define only */
Text2 = "Bird"; /* Point to some text */
<a href=../FUNCTIONS/printf.html>printf</a>("%s%s\n", Text1, Text2);
}
</pre>
</td></tr>
<tr align=center><td>
<b>Result</b>
</td></tr><tr><td>
<pre>
ThunderBird
</pre>
</td></tr></table>
<p>
This is all very well, but there is a MAJOR problem! <b>Thunder</b>
and <b>Bird</b> are
<a href=constants.html>constants</a>, 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
<a href=../FUNCTIONS/malloc.html>malloc</a> to do just that.
<p>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href=../SYNTAX/void.html>VOID keyword.</a>
<hr>
<p>
<center>
<table border=2 width=80% bgcolor=ivory>
<tr align=center>
<td width=25%>
<a href=../cref.html>Top</a>
</td><td width=25%>
<a href=../master_index.html>Master Index</a>
</td><td width=25%>
<a href=../SYNTAX/keywords.html>Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
<script language="JavaScript">
<!-- //
document.write(document.lastModified);
// -->
</script>
</address>
|