Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
C (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Pointers === C supports the use of [[Pointer (computer programming)|pointers]], a type of [[Reference (computer science)|reference]] that records the address or location of an object or function in memory. Pointers can be ''dereferenced'' to access data stored at the address pointed to, or to invoke a pointed-to function. Pointers can be manipulated using assignment or [[pointer arithmetic]]. The run-time representation of a pointer value is typically a raw memory address (perhaps augmented by an offset-within-word field), but since a pointer's type includes the type of the thing pointed to, expressions including pointers can be type-checked at compile time. Pointer arithmetic is automatically scaled by the size of the pointed-to data type. Pointers are used for many purposes in C. [[Text string]]s are commonly manipulated using pointers into arrays of characters. [[Dynamic memory allocation]] is performed using pointers; the result of a <code>malloc</code> is usually [[Type conversion|cast]] to the data type of the data to be stored. Many data types, such as [[Tree (data structure)|trees]], are commonly implemented as dynamically allocated <code>struct</code> objects linked together using pointers. Pointers to other pointers are often used in multi-dimensional arrays and arrays of <code>struct</code> objects. Pointers to functions (''[[function pointer]]s'') are useful for passing functions as arguments to [[higher-order function]]s (such as [[qsort]] or [[bsearch]]), in [[dispatch table]]s, or as [[callbacks]] to [[event handler]]s.<ref name="bk21st" /> A ''[[null pointer]] value'' explicitly points to no valid location. Dereferencing a null pointer value is undefined, often resulting in a [[segmentation fault]]. Null pointer values are useful for indicating special cases such as no "next" pointer in the final node of a [[linked list]], or as an error indication from functions returning pointers. In appropriate contexts in source code, such as for assigning to a pointer variable, a ''null pointer constant'' can be written as <code>0</code>, with or without explicit casting to a pointer type, as the <code>NULL</code> macro defined by several standard headers or, since C23 with the constant <code>nullptr</code>. In conditional contexts, null pointer values evaluate to <code>false</code>, while all other pointer values evaluate to <code>true</code>. Void pointers (<code>void *</code>) point to objects of unspecified type, and can therefore be used as "generic" data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them allowed, although they can easily be (and in many contexts implicitly are) converted to and from any other object pointer type.<ref name="bk21st" /> Careless use of pointers is potentially dangerous. Because they are typically unchecked, a pointer variable can be made to point to any arbitrary location, which can cause undesirable effects. Although properly used pointers point to safe places, they can be made to point to unsafe places by using invalid [[pointer arithmetic]]; the objects they point to may continue to be used after deallocation ([[dangling pointer]]s); they may be used without having been initialized ([[wild pointer]]s); or they may be directly assigned an unsafe value using a cast, union, or through another corrupt pointer. In general, C is permissive in allowing manipulation of and conversion between pointer types, although compilers typically provide options for various levels of checking. Some other programming languages address these problems by using more restrictive [[Reference (computer science)|reference]] types.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
C (programming language)
(section)
Add topic