source: public/doc/gnu-c/Void-Pointers.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.0 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Void Pointers (GNU C Language Manual)</title>
23
24<meta name="description" content="Void Pointers (GNU C Language Manual)">
25<meta name="keywords" content="Void Pointers (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Pointers.html" rel="up" title="Pointers">
33<link href="Pointer-Comparison.html" rel="next" title="Pointer Comparison">
34<link href="Invalid-Dereference.html" rel="prev" title="Invalid Dereference">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Void-Pointers"></span><div class="header">
59<p>
60Next: <a href="Pointer-Comparison.html" accesskey="n" rel="next">Pointer Comparison</a>, Previous: <a href="Invalid-Dereference.html" accesskey="p" rel="prev">Invalid Dereference</a>, Up: <a href="Pointers.html" accesskey="u" rel="up">Pointers</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Void-Pointers-1"></span><h3 class="section">14.8 Void Pointers</h3>
64<span id="index-void-pointers"></span>
65<span id="index-pointers_002c-void"></span>
66
67<p>The peculiar type <code>void *</code>, a pointer whose target type is
68<code>void</code>, is used often in C. It represents a pointer to
69we-don&rsquo;t-say-what. Thus,
70</p>
71<div class="example">
72<pre class="example">void *numbered_slot_pointer (int);
73</pre></div>
74
75<p>declares a function <code>numbered_slot_pointer</code> that takes an
76integer parameter and returns a pointer, but we don&rsquo;t say what type of
77data it points to.
78</p>
79<p>With type <code>void *</code>, you can pass the pointer around and test
80whether it is null. However, dereferencing it gives a <code>void</code>
81value that can&rsquo;t be used (see <a href="The-Void-Type.html">The Void Type</a>). To dereference the
82pointer, first convert it to some other pointer type.
83</p>
84<p>Assignments convert <code>void *</code> automatically to any other pointer
85type, if the left operand has a pointer type; for instance,
86</p>
87<div class="example">
88<pre class="example">{
89 int *p;
90 /* <span class="roman">Converts return value to <code>int *</code>.</span> */
91 p = numbered_slot_pointer (5);
92 <span class="roman">&hellip;</span>
93}
94</pre></div>
95
96<p>Passing an argument of type <code>void *</code> for a parameter that has a
97pointer type also converts. For example, supposing the function
98<code>hack</code> is declared to require type <code>float *</code> for its
99argument, this will convert the null pointer to that type.
100</p>
101<div class="example">
102<pre class="example">/* <span class="roman">Declare <code>hack</code> that way.</span>
103 <span class="roman">We assume it is defined somewhere else.</span> */
104void hack (float *);
105&hellip;
106/* <span class="roman">Now call <code>hack</code>.</span> */
107{
108 /* <span class="roman">Converts return value of <code>numbered_slot_pointer</code></span>
109 <span class="roman">to <code>float *</code> to pass it to <code>hack</code>.</span> */
110 hack (numbered_slot_pointer (5));
111 <span class="roman">&hellip;</span>
112}
113</pre></div>
114
115<p>You can also convert to another pointer type with an explicit cast
116(see <a href="Explicit-Type-Conversion.html">Explicit Type Conversion</a>), like this:
117</p><div class="example">
118<pre class="example">(int *) numbered_slot_pointer (5)
119</pre></div>
120
121<p>Here is an example which decides at run time which pointer
122type to convert to:
123</p>
124<div class="example">
125<pre class="example">void
126extract_int_or_double (void *ptr, bool its_an_int)
127{
128 if (its_an_int)
129 handle_an_int (*(int *)ptr);
130 else
131 handle_a_double (*(double *)ptr);
132}
133</pre></div>
134
135<p>The expression <code>*(int *)ptr</code> means to convert <code>ptr</code>
136to type <code>int *</code>, then dereference it.
137</p>
138<hr>
139<div class="header">
140<p>
141Next: <a href="Pointer-Comparison.html" accesskey="n" rel="next">Pointer Comparison</a>, Previous: <a href="Invalid-Dereference.html" accesskey="p" rel="prev">Invalid Dereference</a>, Up: <a href="Pointers.html" accesskey="u" rel="up">Pointers</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
142</div>
143
144
145
146</body>
147</html>
Note: See TracBrowser for help on using the repository browser.