POINTERS Unit 4
POINTERS Unit 4
Q) What is pointer?
Pointers are variables that hold address of another variable of same data type.
(OR)
A pointer is a variable whose value is the address of another variable.
Pointers are used in C program to access the memory and manipulate the address.
data_type *pt_name;
Data_type: It specifies the type of pointer. It can be int,char, float etc. This type specifies the type
of variable whose address this pointer can store.
The * tells that the variable pt_name is a pointer variable.
pt_name means a memory location.
int a = 10 ;
int *ptr ; //pointer declaration
ptr = &a ; //pointer initialization
or,
int *ptr = &a ; //initialization and declaration together
1
Example:
#include <stdio.h> printf("Address of c:%u\n",&c);
void main() printf("Value of c:%d\n\n",c);
{ getch();
int * pc; }
int c;
c=22;
clrscr();
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
Q) Null pointer:
It is special pointer value that does not point any valid memory address. To declare null pointer
you may use NULL keyword.
int *ptr=NULL;
Q) Generic pointer:
It is pointer variable that has void as its data type.
It is a special type of pointer that can be used to point to variables of any data type.
void *ptr;
In C programming, we can pass the address of the variable to the formal arguments of a
function. This method of calling a function by passing pointer arguments is known as call by
reference. Any change in the value of formal parameters inside function will effect the value of actual
argument.
Example: #include<stdio.h>
2
void swap(int *,int *);
void main()
{
int a,b;
printf(“enter the two values of a & b\n”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
printf(“after interchanging the values are %d & %d\n”,a,b);
}
swap(int *x,int *y)
{
int t;
t=*x;
*x= *y;
*y= t;
Q) Array of pointers:
1) Static allocation: -
When we declare a static or global variable, static allocation is done for the variable. Each static
variable is allocated a fixed size of memory space, this memory cannot be changed during the
execution of a program.
2) Automatic allocation:
Automatic memory allocation is done for function arguments or local variable The allocated space
for automatic variable is allocated when the compound statement congaing the declaration is entered
and is freed when it exists from a compound statement.
3) Dynamic allocation:
The process of allocating memory to the variable during the execution of a program is called dynamic
memory allocation.
C language supports 4 kinds of library functions under "stdlib.h" for dynamic memory allocation.
Function Description
malloc() (memory allocates requested size of bytes and returns a void pointer pointing to
allocation) the first byte of the allocated space
calloc()(contiguous allocates space for an array of elements, initialize them to zero and
allocation) then return a void pointer to the memory