Computer Programming C: UNIT III: Functions and Pointers

Example C programs using pointers with functions, arrays and structures

Sample Simple Example C programs using pointers with functions, arrays and structures.

CASE STUDIES

(Example programs using pointers with functions, arrays and structures)

 

1. Write a program to interchange the values stored in two variables using function with pointers.

The below program shows the contents of two variables, which can be interchanged using their address locations.

The function interchange() receives the addresses of the two variables 'a' and 'b' and exchanges their contents.

 /* Program to interchange the values stored in two variables using function with pointers */

#include <stdio.h>

main()

{

/* Local definitions */

void interchange(int *, int *); /* interchange is a function with integer pointer variable */

int a=39, b=77;

/* Statements */

printf("\nBefore Interchange: a = %d, b = %d\n",a,b);

interchange(&a,&b);  /* function call */

printf("After Interchange : a = %d, b %d, b = %d\n",a,b);

} /* main */

void interchange (int *a, int *b)

{

/* Local definitions */

int t;

t=*a;

*a= *b;

*b=t;

} /* interchange */

OUTPUT

Before Interchange: a = 39, b = 77

After Interchange : a = 77, b = 39

EXPLANATION: In the above program the variable a is initialised as value 39 and b is initialised as value 77, then prints the values of a and b then call the user defined function interchange() using address location of a and b. In the user‒defined function variable a and b is declared as pointer variable. The body of function interchange the value a to b and b to a using third variable t then control transfer to the main program and prints the value of a and b after interchanged.

 

2. Write a program to demonstrate the use of structure pointers.

While using structure pointer, be sure and take care of the precedence of operators, because the operators '‒>', '.', '()', and '[ ]' are frequently used in structure pointers.

/* Program to demonstrate the use of structure pointers */

#include <stdio.h>

/* Structure book consists of bno, cost and pointer *bname[] */

struct book

{

        int bno;

        char *bname [20];

        int cost;

}

main()

{

/* Local definitions */

struct book_p[3],*ptr;

printf("\nEnter book details.....\n");

printf("Enter Book Number, Name, Cost..");

for(ptr=p;ptr<p+3;ptr ++)

        scanf("%d %s %d", &ptr‒>bno, ptr‒> bname, &ptr‒> cost);

        printf("STRUCTURE OUTPUT \n");

        ptr=p;

while (ptr<p+3)

{

        printf("%5d\t%‒20s \t%d\n",ptr‒>bno,ptr‒>bname, ptr‒> cost);

         ptr++;

} /* while */

getch();

} /* main */

OUTPUT

Enter book details.....

Enter Book Number, Name, Cost..

101 COMPUTERPRACTICE_1  240

201 COMPUTERPRACTICE_2 250

202 C++ AND JAVA

STRUCTURE OUTPUT

101 COMPUTERPRACTICE_1 240

201 COMPUTERPRACTICE_2 250

202 C++ AND JAVA 280

EXPLANATION: In main function ptr declared as a pointer. Using for loop the variable bno, bname and cost are read, then p is assigned to the pointer variable ptr. The while loop checks the condition (ptr<p+3) or not. If condition satisfied then prints, bno, bname, cost using pointer variable ptr and pointer variable get increment else the program terminated.

 

3. Write a program to demonstrate malloc() function.

/* Program to demonstrate malloc() function */

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <process.h>

int main(void)

{

Local definitions */

char *str;

/* Statements */

clrscr();

if ((str= (char *)malloc(10)) == NULL)   /* memory allocation to str */

{

      printf("Not enough memory to allocate buffer\n");

     exit(1);

} /* if */

strcpy(str, "Hello"); /*String copy */

printf("String is %s\n", str);

free (str); /* free the memory space */

getch();

return 0;

} /* main */

OUTPUT

String is Hello

EXPLANATION: In the above program, the pointer variable str is declared as a character type variable. The if statement checks the condition (str=(char*)malloc(10)==NULL) or not, if condition true then prints the statement "Not enough memory to allocate buffer" and exit. Here, in the declaration 10 bytes are allocated to pointer variable str of type char. The strcpy function copy the string Hello to pointer variable str then prints the string str and make free the memory space fo str variable.

 

4. Write a program to demonstrate calloc() function.

 /*Program to demonstrate calloc() function */

#include <stdio.h>

#include <alloc.h>

#include <string.h>

int main(void)

{

/* Local definitions */

char *str NULL;      /* *str is a character pointer */

str (char *) calloc(10, sizeof(char));

/* Statements */

strcpy(str, "MSSM"); /* string copy */

printf("String is %s\n", str);

free(str); /* free the memory */

return 0;

} /* main */

OUTPUT

String is MSSM

EXPLANATION: In the above program, the variable str is declared as a pointer variable. The statement ((char*)calloc(10, sizeof(char))) is assigned to pointer variable str using assignment operator. The calloc() function is used for allocating memory space. The string MSSMis copied to str using strcpy function, then prints the string and make free the pointer variable str.

 

5. Write a program to demonstrate realloc() function.

/* Program to demonstrate realloc() function */

#include <stdio.h>

#include <alloc.h>

#include <string.h>

int main(void)

{

/* Local definitions */

char *str;

str = (char *)malloc(10);

/* Statements */

strcpy(str, "BHAVANA"); /* string copy */

printf("String is %s\n Address is %p\n", str, str);

str = (char *) realloc(str, 20);

printf("String is %s\n New address is %p\n", str, str);

free(str); /* free the memory */

return 0;

} /* main */

OUTPUT

String is BHAVANA

        Address is 05E4

String is BHAVANA

       New address is 05F2

EXPLANATION: In the above program, using malloc() function 10 bytes are allocated to character str. The character pointer is initialised store more that 10 characters we need to allocate more bytes to pointer str using realloc() function memory reallocation takes place. After reallocation the pointer contains 20 bytes then prints the string str and address. The free() function releases the allocated memory.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : - Example C programs using pointers with functions, arrays and structures


Computer Programming C: UNIT III: Functions and Pointers



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation