Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

PROGRAMMING AND PROBLEM SOLVING

(MODULE 1- POINTERS & ARRAYS)


PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.1 POINTER TO POINTERS :
In C, we can also define a pointer to store the address of another pointer. This creates many layers of pointer and
therefore called as pointers to pointers (Double pointer / Multiple Indirection).
The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the
first pointer. Let's understand it by the diagram given below.

Consider the following example


#include <stdio.h>
#include <conio.h>
void main() 
{
  int i =100;
  int **pt1;
  int *pt2;
  pt2 = &i;
  pt1 = &pt2;
  printf("The value of **pt1 = %d" + "The value of *pt2 = %d", **pt1, *pt2);
  getch();
}

In the above example we have declared an integer variable i initialized to 100. The statement int ** pt creates a pointer
which points to pointer to a variable with int value. We have passed the value of i to pointer pt2 and to pass the value
of pt2 to pt1, we have used the statement pt1 = &pt2; which shows the Multiple Indirection.
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.2 POINTER TO FUNCTIONS :
• A pointer to a function points to the address of the executable code of the function.
EXAMPLE :
#include <stdio.h>
#include <string.h>
void check(char *a, char *b,
int (*cmp)(const char *, const char *));
int main(void)
{
char s1[80], s2[80];
int (*p)(const char *, const char *);
p = strcmp;
gets(s1);
gets(s2);
check(s1, s2, p);
return 0;
}
void check(char *a, char *b,
int (*cmp)(const char *, const char *))
{
printf("Testing for equality.\n");
if(!(*cmp)(a, b)) printf("Equal");
else printf("Not Equal");
}
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.3 POINTER COMPARISON :
#include <stdio.h>
int main()
{
int *p2;
int *p1;
p2 = (int *)300;
p1 = (int *)200;
if(p1 > p2)
{
printf("P1 is greater than p2");
} else
{
printf("P2 is greater than p1");
}
return(0);
}
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.4 DYNAMIC ALLOCATION
• In dynamic allocation, memory is allocated at run time.
• In dynamic allocation, memory can be increased while executing program
• Dynamic allocation makes efficient use of memory by allocating the required amount of
memory whenever is needed.
• Dynamic allocation allows building complex data structures such as linked lists.
• Dynamic allocation takes place in the heap segment of memory.
• C provides the following dynamic allocation :
(1) malloc() : allocates single block of requested memory.
(2) calloc() : allocates multiple block of requested memory.
(3) realloc() : reallocates the memory occupied by malloc() or calloc() functions.
(4) free() : frees the dynamically allocated memory.
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.5 PASSING SINGLE DIMENSION ARRAYS TO FUNCTION
i n t main ( v o i d )
{
inti[10];
func1(i);
.
.
}
If a single dimension array is passing to afunction may declare its formal parameter in one of three ways: as a pointer, as a sized array, or as an
unsized array.
void func1 (int*x) /* pointer */
{
.
.
}
or
void func1 (intx [10]) /* size d array */
{
.
.
}
or finally as
void func1 (intx [ ]) /* unsized array */
{
.
.
.
}

All three declaration methods produce similar results The first declaration actually uses a pointer , second employs the standard array
declaration and the third is a modified version of an array declaration.

You might also like