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
|
<title>Idioms</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Idioms</h1>
</center>
<hr>
<p>
Here are some C idioms that may be usefull.
<p><hr align=center width="50%"><p>
Place <code>\0</code> at the location pointed to by <code>ptr</code>
then increment <code>ptr</code>
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
*ptr++ = '\0';
</pre>
</td></tr>
</table>
</center>
<p><hr align=center width="50%"><p>
Increment <code>ptr</code> then
place <code>\0</code> at the location pointed to by <code>ptr</code>
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
*++ptr = '\0';
</pre>
</td></tr>
</table>
</center>
<p><hr align=center width="50%"><p>
This program will print its self! I guess its not of any real
use, but I think its clever.
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
main(a) {a="main(a) {a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}
</pre>
</td></tr>
</table>
</center>
<p><hr align=center width="50%"><p>
This is something I saw out on the web. It swaps the value of two
variables without
using a third variable as a temporary store.
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
one ^= two;
two ^= one;
one ^= two;
</pre>
</td></tr>
</table>
</center>
<a name=printf>
<p><hr align=center width="50%"><p>
Have you ever had a SEGV from <a href="../FUNCTIONS/printf.html">printf</a>
because you passed a NULL pointer to a %s flag???. This idiom will put
a stop to all that nonsence.
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
printf("%s\n", Str ? Str : "Null");
</pre>
</td></tr>
</table>
</center>
<p><hr align=center width="50%"><p>
<p>
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href=../EXAMPLES/swap.c>Program swapping the contents of two variables</a>.
<p>
<hr>
<h2>See Also:</h2>
<img src="../../GRAPHICS/whiteball.gif" alt="o">
<a href="got_ya.html">Common Coding Errors</a>.<p>
<p>
<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="keywords.html"> Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|