POINTER
Pointer is a variable that stores the
address of another variable. A pointer in c
is used to allocate memory dynamically at
run time. The pointer variable might be
belonging to any of the data type such as
int, float, char, double, short etc.
Pointer syntax:
data_type*var_name;
DECLARING POINTER VARIABLE
Pointers variables contain addresses that belong
to a separate data type, they must be declared as
pointers before we use them. The declaration of a
pointer variable takes the following form
data_type*
pt_name;
INITIALIZATION OF POINTER
VARIABLE
Pointer initialization is the process of assigning
address of a var to pointer variable. Pointer variable
contains address of variable of same data type. In c
address operator & is used to determined the address
of a variable. The & returns the address of the variable.
EXAMPLE:
int a=10;
int*ptr; //pointer declaration
ptr=&a; //pointer initialization
Or
int*ptr=&a; //initialization and declaration together
EXAMPLE PROGRAM
#include <stdio.h> Output:
int main() 50
{
int *ptr, q; //declaration
q = 50;
ptr=&q; //initialization
printf("%d", *ptr); //display q's value using ptr variable
printf("%d", *ptr);
return 0;}
CHAIN OF POINTER
It is possible to make a pointer to point to another pointer, creating a chain of
pointers.
P2
ADDRES ADDRESS P1 VARIABLE
VALUE
S 1
We can access the target value indirectly pointed to by pointer to a pointer by
applying the indirection operator twice.
main()
{
int x, p1, **p2;
x = 100;
p1 = &x; /* address of x */
p2 = &p1; /* address of pl*/
printf("%d", **p2);
}
POINTER EXPRESSIONS
The pointer variable can be used in expressions. (e
g) p1 and p2 properly declared and initialized
pointer, then the following statements are valid.
y = * p1 **p2;
sum = sum +* p1;
*p2 = *p2+10;
SYNTAX:
data_type*ptr=expression
RULES OF POINTER OPERATIONS
A pointer variable can be assigned the address of
another variable.
A pointer variable can be assigned the values of
another pointer variable.
A pointer variable can be initialized with NULL or
zero value.
A pointer variable can be pre-fixed or post-fixed
with increment or decrement operators.
An integer value may be added or subtracted from
a pointer variable.
ARRAY OF POINTER
Output:
Value of var[0] = 10
#include <stdio.h>
const int MAX = 3; Value of var[1]=100
int main () Value of var[2] =
{ 200
int var[ ] = {10, 100, 200};
int i, *ptr[MAX];
for (i=0; i < MAX; i++)
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for (i=0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
POINTER AS FUNCTION ARGUMENTS
When we pass addresses to a function, the parameters receiving the addresses
should be pointers. The process of calling a function pointers to pass the
addresses of variables is known as 'call by reference
for example:
main()
{
int *x;
* x = 20 ;
change(&x); //call by reference or address
printf("%d\n",x);
}
change(int *p)
{
*p= * p + 10 ;
}
POINTER AND STRUCTURE
Address of Pointer variable can be obtained using '&'
operator.
Address of such Structure can be assigned to the Pointer
variable.
Pointer Variable which stores the address of Structure must
be declared as Pointer to Structure.
Pointer to Structure Syntax:
struct student database
{
char name[10];
int roll;
int marks;
}stud1;
struct student_database *ptr;
EXAMPLE PROGRAM
Output
#include <stdio.h>
int main()
NAME:
{ Mukesh
struct my_structure NUMBER:
{ 433
char name[20]; RANK: 1
int number;
int rank;
}:
struct my_structure variable (“Mukesh",433,1);
struct my structure *ptr;
ptr = &variable;
printf("NAME: %s\n",ptr->name);
printf("NUMBER: %d\n",ptr number);
printf("RANK: %d",ptr->rank);
return 0;
}