POINTERS IN C
Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.
On incrementing a pointer, it points to the immediately next location of its type. The way a pointer can be incremented it can be decremented as well, to point to earlier locations.
Pointer Syntax: data_type *var_name;
Example: int *p; char *p; where * is used to denote that “p” is pointer variable and not a normal variable.
Example: int *p; char *p; where * is used to denote that “p” is pointer variable and not a normal variable.
(&) OPERATOR
We can display the address of a variable using the ampersand sign. & operator is used to access the address of variable num. The & operator is also known as “Address of” Operator. Pointer is just like another variable, the main difference is that it stores address of another variable rather than a value.
(*) OPERATOR
"Dereference Operator" or "Indirection Operator denoted by an asterisk character (*) is a unary operator which performs two operations with the pointer (which is used for two purposes with the pointers).
1. To declare a pointer.
2. To access the stored value of the memory (location) pointed by the pointer.
2. To access the stored value of the memory (location) pointed by the pointer.
EXAMPLE:-
#include<stdio.h>
int main()
{
int *p;
int var =10;
p=&var;
print("Value of variable var is: %d", var);
print("\nValue of variable var is: %d", *p);
print("\nAddress of variable var is: %p", &var);
print("\nAddress of variable var is: %p", p);
print("\nAddress of pointer p is: %p", &p);
return 0;
}
OUTPUT:-
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 1001
Address of variable var is: 1001
Address of pointer p is: 1002
0 Comments
Doubts? Please let our team know So that we can serve you better.