Concept of pointer
Pointers provide a powerful and flexible way to manipulate data.
When we use a variable in a program the compiler reserves a memory location for that variable.
Example int n=20;
When this statement executes, the compiler Reserves space in memory to hold an integer value that is 2 bytes for integer variable.
Associates the name ‘n’ with this memory location.
Stores the value 20 at this location.
In the above figure, 2 bytes of memory from address 1002 have been allotted to variable ‘n’ by assuming that an integer requires 2 bytes of storage.
Hence address of variable ‘n’ is 1002.
The address of operator (&)
The ‘&’ operator gives the address of a variable. It is a unary operator which must be written before the variable name.
Syntax: &variable
Example: ptr=&n;
This statement assigns the address of ‘n’ to a variable called ptr.
Variable and pointer
n ptr
20 1002
1002 2050
In the above example, &n will give 1002.
Explore more in pointers
- What is the pointer
- Value VS Reference Model
- Application of pointers
- Declaring pointers
- Initializing pointers
- Using a pointer
- Illustrate basic pointer use
- Types of pointers
- Pointer Arithmetic
- Pointer to array
- Relationship between Arrays and Pointers
- Pointer to pointer multiple indirection
- Initializing pointer to pointer
- Functions and Pointers
- Some pointer declaration and their meaning
- Pointer const and Constant pointer
- Dynamic memory allocation
- Memory allocation for 2D array
- Memory leaks and dangling pointers
- Static and dynamic memory allocation
Comments
Post a Comment
Please give us feedback through comments