156 lines
4.9 KiB
HTML
156 lines
4.9 KiB
HTML
<html>
|
|
<head>
|
|
<title>
|
|
C Guide--1.4 References
|
|
</title>
|
|
<!-- Changed by: eric huss, 12-Mar-1997 -->
|
|
</head>
|
|
<body text="#000000" bgcolor="#FFFFFF">
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="1.3.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
1.3 Functions</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="1.5.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
1.5 Operators</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<hr>
|
|
<h1>1.4.1 Pointers and the Address Operator</h1>
|
|
<p>
|
|
Pointers are variables that contain the memory address for another variable. A pointer is
|
|
defined like a normal variable, but with an asterisk before the variable name. The type-specifier
|
|
determines what kind of variable the pointer points to but does not affect the actual pointer.
|
|
<p>
|
|
The address operator causes the memory address for a variable to be returned. It is
|
|
written with an ampersand sign before the variable name.
|
|
<p>
|
|
When using a pointer, referencing just the pointer such as:
|
|
<blockquote><code><b>
|
|
int *my_pointer;<br>
|
|
int barny;<br>
|
|
my_pointer=&barny;<br>
|
|
</b></code></blockquote>
|
|
Causes my_pointer to contain the address of barny. Now the pointer can be use
|
|
indirection to reference the variable it points to. Indirection is done by prefixing an asterisk to
|
|
the pointer variable.
|
|
<blockquote><code><b>
|
|
*my_pointer=3;
|
|
</b></code></blockquote>
|
|
This causes the value of barny to be 3. Note that the value of <code><b>my_pointer</b></code> is unchanged.
|
|
<p>
|
|
Pointers offer an additional method for addressing an array. The following array:
|
|
<blockquote><code><b>
|
|
int my_array[3];</b></code>
|
|
</blockquote>
|
|
Can be addressed normally such as:
|
|
<blockquote><code><b>
|
|
my_array[2]=3;
|
|
</b></code>
|
|
</blockquote>
|
|
The same can be accomplished with:
|
|
<blockquote><code><b>
|
|
*(my_array+2)=3;</b></code>
|
|
</blockquote>
|
|
Note that <code><b>my_array</b></code> is a pointer <i>constant</i>. Its value cannot be modified such as:
|
|
<blockquote><code><b>
|
|
my_array++;</b></code>
|
|
This is illegal.
|
|
</blockquote>
|
|
However, if a pointer variable is created such as:
|
|
<blockquote><code><b>
|
|
int *some_pointer=my_array;</b></code>
|
|
</blockquote>
|
|
Then modifying the pointer will correctly increment the pointer so as to point to the next
|
|
element in the array.
|
|
<blockquote><code><b>
|
|
*(some_pointer+1)=3;</b></code>
|
|
</blockquote>
|
|
This will cause the value of <code><b>my_array[1]</b></code> to be 3. On a system where an <code><b>int</b></code> takes up two bytes, adding 1 to <code><b>some_pointer</b></code> did not actually increase it by 1, but by 2 so that it pointed to the next
|
|
|
|
element in the array.
|
|
<p>
|
|
Functions can also be represented with a pointer. A function pointer is defined in the
|
|
same way as a function prototype, but the function name is replaced by the pointer name
|
|
prefixed with an asterisk and encapsulated with parenthesis. Such as:
|
|
<blockquote><code><b>
|
|
int (*fptr)(int, char);<br>
|
|
fptr=some_function;</b></code>
|
|
</blockquote>
|
|
To call this function:
|
|
<blockquote><code><b>
|
|
(*ftpr)(3,'A');</b></code>
|
|
</blockquote>
|
|
This is equivalent to:
|
|
<blockquote><code><b>
|
|
some_function(3,'A');</b></code></blockquote>
|
|
<p>
|
|
A structure or union can have a pointer to represent it. Such as:
|
|
<blockquote><code><b>
|
|
struct some_structure homer;<br>
|
|
struct some_structure *homer_pointer;<br>
|
|
homer_pointer=&homer;
|
|
</b></code></blockquote>
|
|
This defines homer_pointer to point to the structure homer. Now, when you use the
|
|
pointer to reference something in the structure, the record selector now becomes <code><b>-></b></code> instead of a
|
|
period.<br>
|
|
<blockquote><code><b>
|
|
homer_pointer->an_element=5;</b></code>
|
|
</blockquote>
|
|
This is the same as:
|
|
<blockquote><code><b>
|
|
homer.an_element=5;
|
|
</b></code></blockquote>
|
|
The void pointer can represent an unknown pointer type.
|
|
<blockquote><code><b>
|
|
void *joe;
|
|
</b></code></blockquote>
|
|
This is a pointer to an undetermined type.
|
|
|
|
<h1> 1.4.2 Typecasting</h1>
|
|
<p>
|
|
Typecasting allows a variable to act like a variable of another type. The method of
|
|
typecasting is done by prefixing the variable type enclosed by parenthesis before the variable
|
|
name. The actual variable is not modified.
|
|
<p>
|
|
Example:
|
|
<blockquote><code><b>
|
|
float index=3;
|
|
int loop=(int)index;
|
|
</b></code></blockquote>
|
|
This causes index to be typecasted to act like an <code><b>int</b></code>.
|
|
<hr>
|
|
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="1.3.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
1.3 Functions</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="1.5.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
1.5 Operators</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
</body>
|
|
</html>
|