Pointer
A pointer is a variable that stores the memory address of another variable. In above example ‘ptr’ is a pointer variable because it stores the address of another variable ‘n’. Hence it is called a pointer to n. Since a pointer is a variable, it will also occupy some memory location.
Value of variable ‘n’ can be accessed in two ways:
Using the name of the variable i.e. ‘n’
Using pointer
Reference and De-reference Referencing means taking the address of an existing variable (using &) to set a pointer variable.
The operation of accessing variable using the pointer is called ‘dereference’. For example, accessing variable ‘n’ using ‘ptr’ is called dereference. De-referencing is the operation to access or manipulate data contained in the memory location pointed to by a pointer.
The ‘&’ operator is used for reference. It is also called the ‘address-of ‘operator. The ‘*’ operator is used for dereference. It is also called the ‘value-at’ operator.
Syntax: *pointer
Example:
int n=20;
int *ptr;
ptr = &n; //reference printf(“%d”,*ptr); //deference. Displays 20 Size of a pointer
A pointer is a variable. Hence it occupies some memory. The size of a pointer variable is the same as an unsigned integer (either 2 or 4 bytes).
The size of pointer does not depend on the type of data it points to.
sizeof()operator can be used to find out the size of a pointer variable as shown below: Example:
float *ptr;
printf(“The size of ptr is %d”, sizeof(ptr));
Also Read
- Static and Dynamic Memory Allocation
- Memory Leak and Dangling Pointer
- Memory Allocation for 2D Array
- Dynamic Memory Allocation
- Pointer Constant and Constant Pointer
- Pointer Declarations and their Meanings
- Functions and Pointers
- Initializing Pointer to Pointer
- Pointer to Pointer Multiple Indirection
- Relationship between Array and Pointer
- Pointer to Array
- Pointer Arithmetic
- Types of Pointer
- Illustrate Basic Pointer Use
- Using Pointer
- Intializing Pointer
- Declaring Pointer
- Applications of Pointer
- Value Model and Reference Model
Comments
Post a Comment
Please give us feedback through comments