0% found this document useful (0 votes)
38 views

08 Pointer

Pointer variables contain memory addresses rather than direct values. They are defined with a * and initialized to 0, NULL, or an address. The & operator returns the address of a variable. The * operator dereferences a pointer to access the value at that address. * and & are inverse operators that cancel each other out.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

08 Pointer

Pointer variables contain memory addresses rather than direct values. They are defined with a * and initialized to 0, NULL, or an address. The & operator returns the address of a variable. The * operator dereferences a pointer to access the value at that address. * and & are inverse operators that cancel each other out.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Pointers

Pointer Variable Definitions and Initialization


• Pointer variables
• Contain memory addresses as their values
• Normal variables contain a specific value (direct reference)
count
7
• Pointers contain address of a variable that has a specific value (indirect
reference)
• Indirection – referencing a pointer value

countPtr count
7
Pointer Variable Definitions and Initialization
• Pointer definitions
• * used with pointer variables
int *myPtr;
• Defines a pointer to an int (pointer of type int *)
• Multiple pointers require using a * before each variable definition
int *myPtr1, *myPtr2;
• Can define pointers to any data type
• Initialize pointers to 0, NULL, or an address
• 0 or NULL – points to nothing (NULL preferred)
Pointer Operators
• & (address operator)
• Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; /* yPtr gets address of y */
yPtr “points to” y

y yptr y
5 500000 600000 600000 5
yPtr

Address of y
is value of
yptr
Pointer Operators
• * (indirection/dereferencing operator)
• Returns a synonym/alias of what its operand points to
• *yptr returns y (because yptr points to y)
• * can be used for assignment
• Returns alias to an object
*yptr = 7; /* changes y to 7 */
• Dereferenced pointer (operand of *) must be an lvalue (no constants)
• * and & are inverses
• They cancel each other out

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy