0% found this document useful (0 votes)
12 views4 pages

POINTERS Unit 4

Pointers are variables that hold the address of another variable of the same data type, allowing for memory access and manipulation in C. They can be declared and initialized using the syntax 'data_type *pt_name' and can be used in expressions, pointer arithmetic, and function argument passing. C supports static, automatic, and dynamic memory allocation, with functions like malloc, calloc, free, and realloc for dynamic memory management.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

POINTERS Unit 4

Pointers are variables that hold the address of another variable of the same data type, allowing for memory access and manipulation in C. They can be declared and initialized using the syntax 'data_type *pt_name' and can be used in expressions, pointer arithmetic, and function argument passing. C supports static, automatic, and dynamic memory allocation, with functions like malloc, calloc, free, and realloc for dynamic memory management.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

POINTERS

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.

Q) How to declare and initialize a pointer variable?

Declaration of pointer variable:


The declaration of pointer variable takes the following form:

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.

Example: int *pnum; //integer pointer


char *pch; //character pointer
float *fp; //float pointer

Initialization of pointer variable:

Pointer Initialization is the process of assigning address of a variable to pointer variable.


Pointer variable contains address of variable of same data type. In C language address
operator & is used to determine the address of a variable. The & returns the address of the
variable associated with it.

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) Pointer expression and pointer arithmetic:

 We can use pointer variables in expression.


 We can perform addition and subtraction of integer constant from pointer variable.
 We can not perform addition, multiplication and division operations on two pointer variables.
However we can subtract one pointer variable from another pointer variable.
We can use increment and decrement operator along with pointer variable to increment or
decrement the address contained in pointer variable.
 We can use relational operators to compare pointer variables if both pointer variable points to
the variables of same data type.

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;

Q) Passing arguments to functions using pointers:

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) Pointers and arrays:


A pointer can also point to an array, which may be single dimensional array or double dimensional
array.
Pointer to Single dimensional array:
 When an array is declared the compiler allocates a base address and sufficient amount of
storage for all the elements of the array in contiguous memory locations.
 The base address is the location of the first element of the array.
 The compiler defines the array name as a constant pointer to the first element.
Syntax:
Poniter_variable=arrayname(or)arrayname[0];
e.g. int a[5]={1,2,3,4,5};
p=a or p=&a[0]
p p+1 p+2 p+3 p+4

1002 1004 1006 1008 1010

6000 6002 6004 6006 6008


Now the relationship between p and a is
p=&a[0]=1002
p+1=&a[1]=1004
p+2=&a[2]=1006
p+3=&a[3]=1008
p+4=&a[4]=1010
A program to read and print an array of n elements using pointers.
#include<stdio.h>
main()
{
int a[20],*p,n,i;
printf(“enter n \n”);
scanf(“%d”,&n);
p=a;
printf(“enter %d numbers\n”,n);
3
for(i=0;i<n;i++)
scanf(“%d”,(p+i));
printf(“the given array is \n”);
for(i=0;i<n;i++)
printf(“%d\n”,*(p+i));
}

Q) Array of pointers:

An array of pointers is a collection of pointer variables stored in continuous memory location.


Syntax:
data_type *pointer_name[size];
Example: int *ptr[5];
int p=1,q=2,r=3,s=4,t=5;
ptr[0]=&p; ptr[1]=&q; ptr[2]=&r; ptr[3]=&s; ptr[4]=&t;

Q) Memory allocation in c programs:

C supports 3 types of memory allocations.

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

free releases previously allocated memory

realloc modify the size of previously allocated space

You might also like