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
Pointer (computer programming)
(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!
===C pointers=== The basic [[Syntax (programming languages)|syntax]] to define a pointer is:<ref>[[#c-std|ISO/IEC 9899]], clause 6.7.5.1, paragraph 1.</ref> <syntaxhighlight lang="C">int *ptr;</syntaxhighlight> This declares <code>ptr</code> as the identifier of an object of the following type: * pointer that points to an object of type <code>int</code> This is usually stated more succinctly as "<code>ptr</code> is a pointer to <code>int</code>." Because the C language does not specify an implicit initialization for objects of automatic storage duration,<ref>[[#c-std|ISO/IEC 9899]], clause 6.7.8, paragraph 10.</ref> care should often be taken to ensure that the address to which <code>ptr</code> points is valid; this is why it is sometimes suggested that a pointer be explicitly initialized to the [[null pointer]] value, which is traditionally specified in C with the standardized macro <code>NULL</code>:<ref name="c-NULL">[[#c-std|ISO/IEC 9899]], clause 7.17, paragraph 3: ''NULL... which expands to an implementation-defined null pointer constant...''</ref> <syntaxhighlight lang="C">int *ptr = NULL;</syntaxhighlight> Dereferencing a null pointer in C produces [[undefined behavior]],<ref>[[#c-std|ISO/IEC 9899]], clause 6.5.3.2, paragraph 4, footnote 87: ''If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer...''</ref> which could be catastrophic. However, most implementations{{citation needed|date=July 2011}} simply halt execution of the program in question, usually with a [[segmentation fault]]. However, initializing pointers unnecessarily could hinder program analysis, thereby hiding bugs. In any case, once a pointer has been declared, the next logical step is for it to point at something: <syntaxhighlight lang="C"> int a = 5; int *ptr = NULL; ptr = &a; </syntaxhighlight> This assigns the value of the address of <code>a</code> to <code>ptr</code>. For example, if <code>a</code> is stored at memory location of 0x8130 then the value of <code>ptr</code> will be 0x8130 after the assignment. To dereference the pointer, an asterisk is used again: <syntaxhighlight lang="C">*ptr = 8;</syntaxhighlight> This means take the contents of <code>ptr</code> (which is 0x8130), "locate" that address in memory and set its value to 8. If <code>a</code> is later accessed again, its new value will be 8. This example may be clearer if memory is examined directly. Assume that <code>a</code> is located at address 0x8130 in memory and <code>ptr</code> at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits wide. The following is what would be in memory after the following code snippet is executed: <syntaxhighlight lang="C"> int a = 5; int *ptr = NULL; </syntaxhighlight> {| class="wikitable" style="padding-left: 2em;" ! Address !! Contents |- | '''0x8130''' || 0x00000005 |- | '''0x8134''' || 0x00000000 |} (The NULL pointer shown here is 0x00000000.) By assigning the address of <code>a</code> to <code>ptr</code>: <syntaxhighlight lang="C">ptr = &a;</syntaxhighlight> yields the following memory values: {| class="wikitable" style="padding-left: 2em;" ! Address !! Contents |- | '''0x8130''' || 0x00000005 |- | '''0x8134''' || 0x00008130 |} Then by dereferencing <code>ptr</code> by coding: <syntaxhighlight lang="C">*ptr = 8;</syntaxhighlight> the computer will take the contents of <code>ptr</code> (which is 0x8130), 'locate' that address, and assign 8 to that location yielding the following memory: {| class="wikitable" style="padding-left: 2em;" ! Address !! Contents |- | '''0x8130''' || 0x00000008 |- | '''0x8134''' || 0x00008130 |} Clearly, accessing <code>a</code> will yield the value of 8 because the previous instruction modified the contents of <code>a</code> by way of the pointer <code>ptr</code>.
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)