Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C (programming language)
(section)
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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)