Pointers are a variable that actually stores or contains the address of the other variable. We can have a pointer to any type(int, char, float, etc). A pointer variable is represented by *. It is the most important concept of C.
For example: int a=10;
int *ptr; // pointer of integer type
ptr=&a; // ptr stores the address of a
"&" is called as Address Operator. It is used to get the address of the variable.
For example: int a=10;
int *ptr; // pointer of integer type
ptr=&a; // ptr stores the address of a
"&" is called as Address Operator. It is used to get the address of the variable.
Indirect Access of Values
The indirection operator * accesses an object of a speci ed type at an address Accessing an object by its address is called indirect access Thus *ptr indirectly accesses the object that ptr points to ie *ptr accesses x The indirection operator is also called the contents of an operator or the dereference operator Applying the indirection operator to a pointer variable is referred to as dereferencing the pointer variable ie *ptr dereferences ptr The address of operator is used to get the address of an object.
Let us consider some examples using the following declarations
int x, z;
float y;
char ch, *pch;
int * pi, *pi2;
float *pf;
When these declarations are encountered memory cells are allocated for these variables at some addresses. Variables x and z are int types y is float and ch is char. Pointer variables pi and pi2 are variables that can point to integers, pf is a float pointer and pch is a character pointer. Note that the initial values of all variables including pointer variables are unknown Just as we must initialize int and float variables we must also initialize pointer variables.
Program for understanding the concept of pointer:
#include<stdio.h>
void main()
{
int a=10;
int *ptr;
ptr=&a;
printf("Value of a=%d",a);
printf("Value of a=%d",*ptr);
printf("Address of a=%d",ptr);
printf("Address of a=%d",&a);
printf("Addres of ptr=%d",&ptr);
}
OUTPUT:
Value of a=10
Value of a=10
Address of a=1024
Address of a=1024
Address of ptr=2048
Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable are 4 bytes. Now, when we increment pointer ptr
ptr++;
It will point to next memory location i.e.5004 because it will jump to the next integer location which is 4 bytes next to the current location.
Now, when we decrement ptr
ptr--;
ptr will point to 4996 i.e. previous integer location which is 4 bytes back to the current location.
Incrementing a Pointer
Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable are 4 bytes. Now, when we increment pointer ptrptr++;
It will point to next memory location i.e.5004 because it will jump to the next integer location which is 4 bytes next to the current location.
Decrementing a Pointer
Decrementing a pointer will decrease its value by the number of bytes of its data type.Now, when we decrement ptr
ptr--;
ptr will point to 4996 i.e. previous integer location which is 4 bytes back to the current location.
No comments:
Post a Comment