|
|
1.1 root 1:
2:
3: pointer C Language pointer
4:
5:
6:
7:
8: A pointer is an object whose value is the address of another ob-
9: ject. The name ``pointer'' derives from the fact that its con-
10: tents ``point to'' another object. A pointer may point to any
11: type, complete or incomplete, including another pointer. It may
12: also point to a function, or to nowhere.
13:
14: The term pointer type refers to the object of a pointer. The ob-
15: ject to which a pointer points is called the referenced type.
16: For example, an int * (``pointer to iinntt'') is a pointer type; the
17: referenced type is int. Constructing a pointer type from a
18: referenced type is called pointer type derivation.
19:
20: ***** The Null Pointer *****
21:
22: A pointer that points to nowhere is a null pointer. The macro
23: NULL, which is defined in the header stdio.h, defines the null
24: pointer. The null pointer is an integer constant with the value
25: zero. It compares unequal to a pointer to any object or func-
26: tion.
27:
28: ***** Declaring a Pointer *****
29:
30: To declare a pointer, use the indirection operator `*'. For ex-
31: ample, the declaration
32:
33:
34: int *pointer;
35:
36:
37: declares that the variable pointer holds the address of an iinntt-
38: length object. Likewise, the declaration
39:
40:
41: int **pointer;
42:
43:
44: declares that pointer holds the address of a pointer whose con-
45: tents, in turn, point to an iinntt-length object.
46:
47: Failure to declare a function that returns a pointer will result
48: in that function being implicitly declared as an int. This will
49: not cause an error on microprocessors in which an int and a
50: pointer have the same size; however, transporting this code to a
51: microprocessor in which an int consists of 16 bits and a pointer
52: consists of 32 bits will result in the pointers being truncated
53: to 16 bits and the program probably failing.
54:
55: C allows pointers and integers to be compared or converted to
56: each other without restriction. The COHERENT C compiler flags
57: such conversions with the strict message
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: pointer C Language pointer
70:
71:
72:
73: integer pointer pun
74:
75:
76: and comparisons with the strict message
77:
78:
79: integer pointer comparison
80:
81:
82: These problems should be corrected if you want your code to be
83: portable to other computing environments.
84:
85: See declarations for more information.
86:
87: ***** Wild Pointers *****
88:
89: Pointers are omnipresent in C. C also allows you to use a
90: pointer to read or write the object to which the pointer points;
91: this is called pointer dereferencing. Because a pointer can
92: point to any place within memory, it is possible to write C code
93: that generates unpredictable results, corrupts itself, or even
94: obliterates the operating system if running in unprotected mode.
95: A pointer that aims where it ought not is called a wild pointer.
96:
97: When a program declares a pointer, space is set aside in memory
98: for it. However, this space has not yet been filled with the ad-
99: dress of an object. To fill a pointer with the address of the
100: object you wish to access is called initializing it. A wild
101: pointer, as often as not, is one that is not properly in-
102: itialized.
103:
104: Normally, to initialize a pointer means to fill it with a
105: meaningful address. For example, the following initializes a
106: pointer:
107:
108:
109: int number;
110: int *pointer;
111: . . .
112: pointer = &number;
113:
114:
115: The address operator `&' specifies that you want the address of
116: an object rather than its contents. Thus, pointer is filled with
117: the address of number, and it can now be used to access the con-
118: tents of number.
119:
120: The initialization of a string is somewhat different than the in-
121: itialization of a pointer to an integer object. For example,
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: pointer C Language pointer
136:
137:
138:
139: char *string = "This is a string."
140:
141:
142: declares that string is a pointer to a char. It then stores the
143: string literal This is a string in memory and fills string with
144: the address of its first character. string can then be passed to
145: functions to access the string, or you can step through the
146: string by incrementing string until its contents point to the
147: null character at the end of the string.
148:
149: Another way to initialize a pointer is to fill it with a value
150: returned by a function that returns a pointer. For example, the
151: code
152:
153:
154: extern char *malloc(size_t variable);
155: char *example;
156: . . .
157: example = malloc(50);
158:
159:
160: uses the function malloc to allocate 50 bytes of dynamic memory
161: and then initializes example to the address that malloc returns.
162:
163: ***** Reading What a Pointer Points To *****
164:
165: The indirection operator `*' can be used to read the object to
166: which a pointer points. For example,
167:
168:
169: int number;
170: int *pointer;
171: . . .
172: pointer = &number;
173: . . .
174: printf("%d\n", *pointer);
175:
176:
177: uses pointer to access the contents of number.
178:
179: When a pointer points to a structure, the elements within the
180: structure can be read by using the structure offset operator `-
181: >'. See the entry for -> for more information.
182:
183: ***** Pointers to Functions *****
184:
185: A pointer can also contain the address of a function. For ex-
186: ample,
187:
188:
189: char *(*example)();
190:
191:
192: declares example to be a pointer to a function that returns a
193: pointer to a char.
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
199:
200:
201: pointer C Language pointer
202:
203:
204:
205:
206: This declaration is quite different from:
207:
208:
209: char **different();
210:
211:
212: The latter declares that different is a function that returns a
213: pointer to a pointer to a char.
214:
215: The following demonstrates how to call a function via a pointer:
216:
217:
218: (*example)(_a_r_g_1, _a_r_g_2);
219:
220:
221: _H_e_r_e, _t_h_e `*' _t_a_k_e_s _t_h_e _c_o_n_t_e_n_t_s _o_f _t_h_e _p_o_i_n_t_e_r, _w_h_i_c_h _i_n _t_h_i_s
222: _c_a_s_e _i_s _t_h_e _a_d_d_r_e_s_s _o_f _t_h_e _f_u_n_c_t_i_o_n, _a_n_d _u_s_e_s _t_h_a_t _a_d_d_r_e_s_s _t_o
223: _p_a_s_s _t_o _a _f_u_n_c_t_i_o_n _i_t_s _l_i_s_t _o_f _a_r_g_u_m_e_n_t_s.
224:
225: _A _p_o_i_n_t_e_r _t_o _a _f_u_n_c_t_i_o_n _c_a_n _b_e _p_a_s_s_e_d _a_s _a_n _a_r_g_u_m_e_n_t _t_o _a_n_o_t_h_e_r
226: _f_u_n_c_t_i_o_n. _T_h_e _f_u_n_c_t_i_o_n_s _b_s_e_a_r_c_h _a_n_d _q_s_o_r_t _b_o_t_h _t_a_k_e _f_u_n_c_t_i_o_n
227: _p_o_i_n_t_e_r_s _a_s _a_r_g_u_m_e_n_t_s. _A _p_r_o_g_r_a_m _m_a_y _a_l_s_o _u_s_e _o_f _a_r_r_a_y_s _o_f
228: _p_o_i_n_t_e_r_s _t_o _f_u_n_c_t_i_o_n_s.
229:
230: ***** _P_o_i_n_t_e_r _C_o_n_v_e_r_s_i_o_n *****
231:
232: _O_n_e _t_y_p_e _o_f _p_o_i_n_t_e_r _m_a_y _b_e _c_o_n_v_e_r_t_e_d, _o_r _c_a_s_t, _t_o _a_n_o_t_h_e_r. _F_o_r
233: _e_x_a_m_p_l_e, _a _p_o_i_n_t_e_r _t_o _a _c_h_a_r _m_a_y _b_e _c_a_s_t _t_o _a _p_o_i_n_t_e_r _t_o _a_n _i_n_t,
234: _a_n_d _v_i_c_e _v_e_r_s_a.
235:
236: _P_o_i_n_t_e_r_s _t_o _d_i_f_f_e_r_e_n_t _d_a_t_a _t_y_p_e_s _a_r_e _c_o_m_p_a_t_i_b_l_e _i_n _e_x_p_r_e_s_s_i_o_n_s,
237: _b_u_t _o_n_l_y _i_f _t_h_e_y _a_r_e _c_a_s_t _a_p_p_r_o_p_r_i_a_t_e_l_y. _U_s_i_n_g _t_h_e_m _w_i_t_h_o_u_t _c_a_s-
238: _t_i_n_g _p_r_o_d_u_c_e_s _a _p_o_i_n_t_e_r-_t_y_p_e _m_i_s_m_a_t_c_h.
239:
240: ***** _P_o_i_n_t_e_r _A_r_i_t_h_m_e_t_i_c *****
241:
242: _A_r_i_t_h_m_e_t_i_c _m_a_y _b_e _p_e_r_f_o_r_m_e_d _o_n _a_l_l _p_o_i_n_t_e_r_s _t_o _s_c_a_l_a_r _t_y_p_e_s,
243: _i._e., _p_o_i_n_t_e_r_s _t_o cchhaarrs or iinntt. Pointer arithmetic is quite
244: limited and consists of the following:
245:
246: 11. One pointer may be subtracted from another.
247:
248: 22. An int or a long, either variable or constant, may be added to
249: a pointer or subtracted from it.
250:
251: 33. The operators ++ or -- may be used to increment or decrement a
252: pointer.
253:
254: No other pointer arithmetic is permitted. No arithmetic can be
255: performed on pointers to non-scalar objects, e.g., pointers to
256: functions.
257:
258:
259:
260:
261:
262: COHERENT Lexicon Page 4
263:
264:
265:
266:
267: pointer C Language pointer
268:
269:
270:
271: ***** i8086 Pointers *****
272:
273: Intel designed the i8086 to use a segmented architecture. This
274: means that the i8086 divides memory into 64-kilobyte segments.
275: To program the i8086 requires that you use a specific memory
276: model, which describes how the segments of memory are to be or-
277: ganized.
278:
279: ***** See Also *****
280:
281: C language, data formats, operators, portability
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328: COHERENT Lexicon Page 5
329:
330:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.