ACP Module2
ACP Module2
Declaration of pointer:
Syntax: data type *pt name ;
1) The asterisk (*) tells that the variable pt_name is a pointer variable.
2) pt name needs a memory location to store address of another variable.
3) pt name points to a variable of type data_type.
2 Write difference between (1) Array & Pointer (2) Static memory allocation & Dynamic memory
allocation
Array Pointer
Array is a constant pointer. Pointer variable can be changed.
It refers directly to the elements. It refers address of the variable.
Memory allocation is in sequence. Memory allocation is random.
Allocates the memory space which cannot Allocated memory size can be resized.
resize or reassigned.
It is a group of elements. It is not a group of elements. It is single
variable.
2
Unit 2 Pointer
ptr 0
ptr[0]
ptr[1]
ptr[2]
ptr[3]
ptr[4]
#include<stdio.h>
#include<conio.h>
void main()
int a=10;
float b=5.67;
void *ptr;
ptr &a;
printf("\n Value of Integer variable is %d", *((int *)ptr));
ptr &b;
3
Unit 2 Pointer
int var=311;
int *ptr;
int **pptr;
/* take the address of var */
ptr &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
getch( );
4
Unit 2 Pointer
• Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int n,*ptr,i;
printf("Enter the size of an array:");
scanf("%d",&n);
ptr = (int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
ptr[i] i+1;
for(i=0;i<n;i++)
printf("\n\t%d",ptr[i]);
free(ptr);
getch();
• Syntax of calloc()
ptr = (cast type *)caIIoc(n,eIement-size);
• This statement will allocate contiguous space in memory for an array of n elements. For
example:
• ptr= (int*) calloc((25,sizeof(int));
• Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int n,*ptr,i;
printf("Enter the size of an array:");
scanf("%d",&n);
ptr = (int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
ptr[i] i+1;
for(i=0;i<n;i++)
printf("\n\t%d",ptr[i]);
free(ptr);
getch();
int *ptr,i,n1,n2;
printf("Enter size of array: ");
scanf("%d",&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf("Address of previously allocated memory: ");
for(i=0;i<n1;++i)
printf("%u\t",ptr+i);
6
Unit 2 Pointer
int a,b;
printf("Enter two numbers:");
scanf("%d%d", &a, &b);
swap(&a, &b);
printf("a=%d b=%d", a, b);
getch();
int temp;
temp=*a;
*a=*b;
*b=temp;
sum=sum+*p;
printf("sum=%d", sum);
getch(); }
7
Unit 2 Pointer
• Advantages:
Pointers provide direct access to memory.
Reduces the execution time of the program
Pointer save memory space.
Pointers provide a way to return more than one value to the functions.
Reduces the storage space and complexity of the program
Provides an alternate way to access array elements
Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs
etc.
Pointers allow us to resize the dynamically allocated memory block.
• Disadvantage of Pointer
If pointers are updated with incorrect values, it might lead to memory corruption.
Uninitialized pointers might cause segmentation fault.