Вы находитесь на странице: 1из 22

UNIT IV – FUNCTIONS & POINTERS

FUNCTION

A function is a set of instruction that perform a specified task. The function is


represented using braces “( )”.

Example:

sqrt( )
add( )
strlen( ) etc.,
Advantages of using function

1. Big complex program can be divided into small subtasks.


2. It is very easy to locate and debug errors.
3. Function can be used wherever it is necessary.
4. Functions avoid the repetition of coding in several places. Instead function can be
called wherever it is required.
5. Functions enable a programmer to build a library of repeatedly used routines.

Types of functions
There are two types of functions in C Language. They are
a. Pre-defined functions
b. User Defined functions

Pre-defined functions

The pre-defined function is defined previously in C Library. User cannot understand


internal working of predefined functions. User cannot change or modify predefined
function.

Example:
• sqrt( )
• clrscr( )
• strcmp( )
• strlen( )
• printf( ) etc.,

User-defined functions

The user-defined functions are defined by the user according to the requirement.
User can understand internal working of user defined function. The user can change or
modify user defined functions according to their need.

Example:
• add( )
• subtract( )
• swap( )
• square( )
• insert( ) etc.,

1
Elements of user defined functions
There are three user defined functions used in C Language. They are
1. Function Declaration
2. Function Call
3. Function Definition
Function Declaration
The functions are declared before they defined or called. The declaration indicates
varies types as follows,
• void → When the function does not return any values, it is declared as void.
• int → When the function returns an integer value, it is declared as int.
• float → When the function returns a floating point value, it is declared as float.
• char → When the function returns a character, it is declared as char.
Function Call
Calling or invoking the function where it is required. The function is called by
specifying the name and its parameter.
Function Definition
Function definition is an independent module which contains the working or set of
instructions of the function.
Sample C Program using function
1. Write a C program to add 2-numbers using function
#include<stdio.h>
#include<conio.h>
void add(int,int); //function declaration
void main()
{
int a,b;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
add(a,b); //Function call
getch();
}
void add(int x,int y) //Function Definition
{
int c;
c=x+y;
printf("\nSum=%d",c);
}
Output
Enter 2 numbers: 5 6
Sum = 11
2. Write a C program to add 2-numbers using function (with return values)
#include<stdio.h>
#include<conio.h>
int add(int,int); //function declaration
void main()
{
int a,b,c;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
c=add(a,b); //Function call
2
printf("\nSum=%d",c);
getch();
}
int add(int x,int y) //Function definition
{
int z;
z=x+y;
return(z);
}
Output
Enter 2 numbers: 5 6
Sum = 11

1. Function Declaration
The functions are declared before they defined or called.
Syntax
function_type function_name(parameter_lists);
Here, Function_type → It specifies return type of the function
• void → When the function does not return any values, it is declared as void.
• int → When the function returns an integer value, it is declared as int.
• float → When the function returns a floating point value, it is declared as float.
• char → When the function returns a character, it is declared as char.
Function_name → It specifies name of the function. It should follow the rules of an
identifier.
Parameter_list → It specifies data type of parameters passed through the function
Terminating Symbol (;) → Function declaration should terminate with semicolon.
Example
void add(int,int);
int add(int,int);
2. Function Call
Calling or invoking the function where it is required. The function is called by
specifying the name and its parameter. Function call is of two types.
a. Call by value
b. Call by reference
a. Call by value
When a function is called using value of its argument is called as Call by Value.
When a function is called, its argument passes a value to the function.
Syntax
function_name(actual_parameter1, actual_parameter2... actual_parametern);
Here,
Function_name → It specifies name of the function. It should be same as function name
used in function declaration. Mismatch of function name will cause error.
Actual Parameter_list → It includes variables of the value to be passed from main function
to the called function.
Example
add(a,b);
area(radius);
swap(a,b);

3
1. Write a C program to swap two numbers using call by value.
#include<stdio.h>
#include<conio.h>
void swap(int,int); //Function declaration
void main()
{
int a,b;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
printf("\nValue before swapping");
printf("\na=%d\nb=%d",a,b);
swap(a,b); //Function call i.e., Call by value
getch();
}
void swap(int a,int b) //Function definition
{
int t;
t=a;
a=b;
b=t;
printf("\nValue after swapping");
printf("\na=%d\nb=%d",a,b);

}
Output
Enter 2 numbers: 5 6
Value before swapping
a=5, b=6
Value after swapping
a=6, b=5
b. Call by reference
When a function is called using address of its argument is called as Call by
Reference. When a function is called, argument passes its address to the function.
Syntax
function_name(&actual_parameter1, &actual_parameter2... &actual_parametern);
Here,
Function_name → It specifies name of the function. It should be same as function name
used in function declaration. Mismatch of function name will cause error.

Actual Parameter_list → It includes address of variables whose value is to be passed from


main function to the called function.
Example
add(&a,&b);
area(&radius);
swap(&a,&b);

4
1. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
#include<conio.h>
void swap(int*,int*); //Function declaration
void main()
{
int a,b;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
printf("\nValue before swapping");
printf("\na=%d\nb=%d",a,b);
swap(&a,&b); //Function call i.e., Call by reference
getch();
}
void swap(int *a,int *b) //Function definition
{
int *t;
*t=*a;
*a=*b;
*b=*t;
printf("\nValue after swapping");
printf("\na=%d\nb=%d",*a,*b);

}
Output
Enter 2 numbers: 5 6
Value before swapping
a=5
b=6
Value after swapping
a=6
b=5

3. Function Definition
Function definition is an independent module which contains the working or set of
instructions of the function. Function definition consists of two main parts.
A. Function header.
B. Function body.
Syntax
function_type function_name(parameter_list) //Function header
{
Local variable declaration;
Body of function; //Function Body
Return statement(Optional);
}

5
Parameter List
Parameters are also called as arguments. The parameters are separated by comma (,)
operator. There are two types of parameters.
a. Actual Parameter
Parameters transferred from the main program to the function definition. Parameters
used within main function are called as Actual parameter.
b. Formal Parameter
Parameters used outside the main function is called are Formal parameter. It is used
within the function definition. Parameters transferred from the function to the main
program.

Write a C program to add 2-numbers using function

#include<stdio.h>
#include<conio.h>
int sum(int,int); //Function declaration
void main()
{
int a,b,c;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
c=sum(a,b); //Function Call here a & b → Actual parameters
printf("\nSum=%d",c);
getch();
}
int sum(int x,int y) //Function Definition here x & y → Formal parameters
{
int z;
z=x+y;
return(z);
}
Here,
• a & b inside the main function are Actual parameters
• x & y inside the function definition are Formal parameters
Output
Enter 2 numbers: 5 3
Sum = 8

6
Categories of function
Based on return type and its parameter function may be classified into four types or
categories. They are,
• Function with no argument and no return value.
• Function with argument and no return value.
• Function with no argument and with return value.
• Function with argument and return value.
1. Function with no argument and no return value.
This type of function has no arguments which are to be passed to the called function and
also has no return value. In such situation,
• Getting input value&
• Displaying the result
Will be performed in “called function” itself. Here there is no transfer of values between the
“calling function” and “called function”.
Syntax for function declaration of above type
void function_name(void);
Example for function declaration of above type
void sum(void);
Program
#include<stdio.h>
#include<conio.h>
void sum(void); //Function declaration
void main()
{
sum(); //Function call without arguments
}
void sum() //Function Definition with no return value
{
int a,b,c;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum=%d",c);
getch();
}
Output
Enter 2 numbers: 2 3
Sum = 5
2.Function with argument and no return value.
A function has arguments which are to be passed to the “called function”. Here input
value is read in “calling function” (i.e., Main function) and it is passed to the “called
function”. A function has no return value, in this category obtained result is displayed in
“called function” itself.
Syntax for function declaration of above type
void function_name(data type of parameter list);
Example for function declaration of above type
void sum(int,int);

7
Program
#include<stdio.h>
#include<conio.h>
void sum(int,int); //Function Declaration
void main()
{
int a,b;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
sum(a,b); //Function Call with arguments
getch();
}
void sum(int a,int b) //Function Definition with no return value
{
int c;
c=a+b;
printf("Sum=%d",c);
}

Output
Enter 2 numbers: 4 3
Sum = 7
3. Function with no argument and with return value.
A function has no arguments; in this category “called function” read data from input
terminal but it has return value. Hence result will be displayed in “calling function” which is
return by the “called function”.
Syntax for function declaration of above type
data_type function_name(void);
Example for function declaration of above type
int sum(void); //Returns an integer value
Program
#include<stdio.h>
#include<conio.h>
int sum(void); //Function declaration
void main()
{
int c;
clrscr();
c=sum(); //Function Call with no arguments passed inside it
printf("\nSum=%d",c);
getch();
}
int sum() //Function definition with return value
{
int a,b,c;
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
}
8
Output
Enter 2 numbers: 4 7
Sum = 11

4. Function with argument and with return value.


A function with arguments in which “calling function” read data from input terminal and
pass it to the “called function”. Process is carried out in the “called function” and returns the
result back to the main function. In this category,
• Getting input value&
• Displaying the result
Will be performed in “calling function” (i.e., main function).
Syntax for function declaration of above type
data_type function_name(data type of parameter list);
Example for function declaration of above type

int sum(int,int); //Returns an integer value


Program
#include<stdio.h>
#include<conio.h>
int sum(int,int); //Function declaration
void main()
{
int a,b,c;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
c=sum(a,b); //Function Call with parameters
printf("\nSum=%d",c);
getch();
}
int sum(int a,int b) //Function Definition with return values
{
int c;
c=a+b;
return(c);
}
Output
Enter 2 numbers: 4 7
Sum = 11

9
Recursion
When a function call itself repeatedly until some specified condition is satisfied is
called as recursion. Recursive functions are commonly used in applications in which the
solution to a problem can be expressed in terms of successively applying the same solution
to subsets of the problem.
Program 1. Write a C program to find the factorial of given number using recursion
Factorial of 5 → 5 * 4 * 3 * 2 * 1 = 120
Recursion can be applied by the following logic
fact(5) = 5 * fact(4)
fact(4) = 4 * fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1
Substitute the values of function from bottom to top to obtain fact(5) = 120.
#include<stdio.h>
int fact(int); //Function declaration
void main()
{
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
printf("Factorial of %d = %d",n,fact(n)); //Function Call
getch();
}
int fact(int n) //Function definition
{
if(n!=1)
n=n*fact(n-1); //Recursive function call
return n;
}
Output
Enter number: 5
Factorial of 5 is 120
2. Write a C program to find the sum of n-natural numbers using recursive function
#include<stdio.h>
int sum(int); //Function Declaration
void main()
{
int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Sum = %d",sum(n)); //Function Call
getch();
}
int sum(int n) //Function Definition
{
if(n!=0)
n=n+sum(n-1); //Recursive function call
return n;
}
10
Output
Enter a positive number: 5
Sum=15
Sample C Program using function
1. Amstrong Number
#include<stdio.h>
#include<conio.h>
int amstrong(int);
int sum=0;
void main()
{
int num,temp;
clrscr();
printf("\nEnter the number:");
scanf("%d",&num);
temp=num;
sum=amstrong(num);
if(temp==sum)
printf("%d is Amstrong number",temp);
else
printf("%d is not amstrong number",temp);
getch();
}
int amstrong(int num)
{
int digit;
while(num>0)
{
digit=num%10;
sum=sum+digit*digit*digit;
num=num/10;
}
return(sum);
}
OUTPUT
Enter the number:153
153 is Amstring number
Enter the number: 25
25 is not amstrong number

2. Square and cube of a number using function


#include<stdio.h>
#include<conio.h>
void square(int);
void cube(int);
void main()
{
int num,sq,cu;
clrscr();
printf("\nEnter the number:");
scanf("%d",&num);
square(num);

11
cube(num);
getch();
}
void square(int num)
{
int sq;
sq=num*num;
printf("\nSquare of %d is %d",num,sq);
}
void cube(int num)
{
int cu;
cu=num*num*num;
printf("\nCube of %d is %d",num,cu);
}
OUTPUT
Enter the number: 3
Square of 3 is 9
Cube of 3 is 27
3. Matrix Addition using functions
#include<stdio.h>
#include<conio.h>
void add(int[10][10],int[10][10]); //Function Declaration
int a[10][10],b[10][10],c[10][10],row,col,i,j; //Global Declaration
void main()
{
clrscr();
printf("Enter the no.of rows:");
scanf("%d",&row);
printf("Etner the no.of columns:");
scanf("%d",&col);

printf("Enter the matrix A element:");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter the matrix B element:");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}

add(a,b); //Function Call


getch();

12
}
void add(int a[10][10],int b[10][10]) //Function Definition
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

printf("Addition matrix is:\n");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}

}
OUTPUT
Enter the no.of rows:3
Etner the no.of columns:3
Enter the matrix A element:
1 2 3
4 5 6
7 8 9
Enter the matrix B element:
1 2 3
4 5 6
7 8 9
Addition matrix is:
2 4 6
8 10 12
14 16 18

4. AREA OF GEOMETRIC SHAPES USING SWITCH CASE & FUNCTION


#include<stdio.h>
#include<conio.h>
void circle(float);
void rectangle(float,float);
void triangle(float,float);
void square(float);
void main()
{
int choice;
float side,base,length,breadth,height,radius;
clrscr();
printf("-------------------------\n");
printf(" 1 --> Circle\n");

13
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the radius\n");
scanf("%f",&radius);
circle(radius);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f%f",&breadth,&length);
rectangle(breadth,length);
break;
case 3:
printf("Enter the base and height\n");
scanf("%f%f",&base,&height);
triangle(base,height);
break;
case 4:
printf("Enter the side\n");
scanf("%f",&side);
square(side);
break;
default:
printf("Error in figure code\n");
break;
}
getch();
}
void circle(float radius)
{
float area1;
area1=3.142*radius*radius;
printf("Area of a circle = %f\n",area1);
}
void rectangle(float breadth,float length)
{
float area2;
area2=breadth*length;
printf("Area of a Reactangle = %f\n",area2);
}
void triangle(float base,float height)
{
float area3;
area3=0.5*base*height;
printf("Area of a Triangle = %f\n",area3);
}

14
void square(float side)
{
float area4;
area4=side*side;
printf("Area of a Square=%f\n",area4);
}
OUTPUT
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
1
Enter the radius
30
Area of a circle = 2827.800049
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
20 30
Area of a Reactangle = 600.000000
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
45 80
Area of a Triangle = 1800.000000
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
4
Enter the side
100
Area of a Square=10000.000000
Important Question

15
Two mark
1. Define function.
2. What are the advantages of using functions?
3. What are the types of function?
4. State the difference between predefined and user defined functions with example?
5. List out the elements of function.
6. How to declare a function?
7. Differentiate call by value and call by reference.
8. What is actual and formal parameter? Give example.
9. What are the categories of function?
10. Define recursion.
Detail
1. Explain the function concept with neat example.
2. Explain in detail about elements of function with sample coding.
3. Write a C program to swap two values using call by value and call by reference.
4. Briefly explain about categories of function with sample coding.
5. Define recursion. Write a C program to find factorial of given number using
recursion.
6. Write a C program to find Armstrong number using function.
7. Write a C program to find sum of n-natural numbers using function.
8. Write a C program to find the square and cube of a given number.

16
POINTERS
Definition
A pointer is a variable that stores the address of another variable. A pointer
represents the location of a data item in the memory. It is a derived data type used in C
Language.
Syntax
datatype *variable_name;
Example1
int *ptr;
ptr = &a;
Here, ptr → Pointer Variable, it holds the memory address of another variable ‘a’
&a → Address of variable ‘a’, it is stores in pointer variable ‘prt’
Example2
a → Variable
10 → Value
2000 → Address
Now,
int *p; //Pointer variable
p = &a; //Address of ‘a’ is assigned to pointer
p = 2000
*p = 10
Write a C program to print address and value of a variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p;
p=&a;
printf("\nAddress of a is %u",p);
printf("\nValue of a is %d",*p);
getch();
}
Output
Address of a is 65524
Value of a is 10
Pointer Declaration
A pointer is a variable that contain address of another variable
Syntax
datatype *variable_name;
Example
char name[10]=”Tamil”;
int a=10; //interger float b=15.23; //float
//Characters
int *p; float *p1;
char *p2;
p = &a; p1 = &b;
p2 = &name;
Initialization of the Pointer variable
The process of assigning the address of a variable to the pointer variable is known as
initialization.
Exampleint a=10;
int *ptr = &a; //initialization
17
7.4 Pointer Types
There are various types of pointers used in C Language. They are
a. void pointer
b. Null pointer
c. Pointer-to-pointer
d. Pointer arithmetic / Pointer Expression.
a. void pointer
Pointer that can point to any type of data item is known as void pointer. It is a
general purpose pointer used for pointing to different data types.
Syntax
void *ptr_name
Example
int a=10;
void *ptr;
ptr=&a;
float a=12.6;
void *ptr;
ptr=&a;
char a=’z’;
void *ptr;
ptr=&a;
Here, variable ‘a’ can hold integer or float or character etc.
b. Null pointer
A pointer is said to be a Null pointer, when its right hand side value is zero(0). A
Null pointer can never point a valid data.
Example
int c = 5;
int *a,*b;
a=0; //Null pointer
b = &c;
Here, a → Null pointer, b →Valid pointer
c. Pointer-to-pointer
Usually pointer variable stores the address of another variable. Pointer-to-pointer
stores address of another pointer variable.
Example
*p1 = &a **p2 = &p1 ***p3 = &p2
Variable→ a p1 p2 p3
Value → 10 1000 2000 3000
Address→ 1000 2000 3000 4000
Here,
*p1 → Pointer , **p2, ***p3 → Pointer-to-pointer
p3 = 3000
p2 = 2000
p1 = 1000 *p3 = 2000
*p2 = 1000
*p1 = 10 **p3 = 1000
**p2 = 10
***p3 = 10

18
Write a C program to illustrate pointer-to-pointer concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p1,**p2,***p3;
clrscr();
p1=&a;
p2=&p1;
p3=&p2;
printf("\nValue of a is %d",a);
printf("\nValue of a is %d",*p1);
printf("\nValue of a is %d",**p2);
printf("\nValue of a is %d",***p3);
printf("\n\nAddress of a is %u",&a);
printf("\nAddress of a is %u",p1);
printf("\nAddress of p1 is %u",p2);
printf("\nAddress of p2 is %u",p3);
getch();
}
Output
Value of a is 10, Value of a is 10
Value of a is 10, Value of a is 10
Address of a is 1000, Address of a is 1000
Address of p1 is 2000, Address of p2 is 3000
d. Pointer Arithmetic
Pointer variables can be used in expressions. We can add, subtract the integer using
pointer variables. It is also called as pointer expression.
Example
*c = *a + *b; *c = (*a) + (*b);
*a = *a + 5; or *a = (*a) + 5;
*p = *x * *b; *p = (*x) * (*b);
Write a C program to illustrate pointer arithmetic concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *pa,*pb,*pc;
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
pa=&a;
pb=&b;
pc=&c;
*pc=*pa+*pb;
printf("\nSum=%d",*pc);
getch();
}
Output
Enter 2 numbers: 3 5
Sum = 8
19
Pointer and Arrays
In C Language, there is a strong relationship between pointers and arrays. Most use
of pointers in C is pointer and array. It is notational convenient and increases the program
efficiency.
Declaration
int a[10];
int *pa;
Assignment
pa = &a[0];
Or
pa = a;

pa pa + 1 pa + 2 pa + 3 pa + 4 pa + 5
a
0 1 2 3 4 5
Pointer variable is incremented automatically for further initialization as shown above.
Example

int a[5] = {10, 20, 30, 40, 50};


int *b; //Pointer declaration
b = a; //Initialization of an array to pointer
0 1 2 3 4
A 10 20 30 40 50
4000 4002 4004 4006 4008
When pointer variable is incremented by 1, related base address will be incremented by
(address + 2) because above declared type in an “integer”.
Write a C program to illustrate pointer and array concept.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50};
int i,*ptr[5]; //Pointer & array declaration
clrscr();
for(i=0;i<5;i++)
{
ptr[i]=&a[i]; //Initialization
}
for(i=0;i<5;i++)
{
printf("\nValue of a[%d] = %d",i,*ptr[i]);
}
getch();
}
Output
Value of a[0] = 10, Value of a[1] = 20
Value of a[2] = 30, Value of a[3] = 40
Value of a[4] = 50
20
Pointer and Function
Pointer can be used as arguments in function. Pointer when passed within the
function, it is called as Call by reference or Pass by reference.
Example
swap(&a, &b);
add(&a, &b);
subtract(&x, &y); etc.,
Write a C program to perform arithmetic operations of two numbers using pointer and
functions
#include<stdio.h>
#include<conio.h>
void add(int *,int *);
void sub(int *,int *);
void mul(int *,int *);
void main()
{
int a,b,choice;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
printf("1.Addition\n2.Subtraction\n3.Multiplication\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add(&a,&b);
break;
case 2:
sub(&a,&b);
break;
case 3:
mul(&a,&b);
break;
default:
printf("Enter correct choice");
break;
}
getch();
}
void add(int *a,int *b)
{
int *add;
*add=*a+*b;
printf("\nSum=%d",*add);
}
void sub(int *a,int *b)
{
int *sub;
*sub=*a-*b;
printf("\nSubtraction=%d",*sub);
}

21
void mul(int *a,int *b)
{
int *mul;
*mul=*a**b;
printf("\nMultiplication=%d",*mul);
}
Output
Enter 2 numbers: 5 6
1.Addition
2.Subtraction
3.Multiplication
Enter your choice: 1
Sum=11
Enter 2 numbers: 5 6
1.Addition
2.Subtraction
3.Multiplication
Enter your choice: 2
Subtraction=-1
Enter 2 numbers: 5 6
1.Addition
2.Subtraction
3.Multiplication
Enter your choice: 3
Multiplication=30
Advantage of Pointer
1. Pointers are most efficient in handling arrays and functions.
2. Pointer allows dynamic memory management.
3. A pointer reduces length and complexity of program.
4. Pointer increases the execution speed and reduces the program execution time.
5. Pointer provide an efficient tool for manipulation dynamic data structure such as
stack, queue, linked lists, structures etc.,
Important Question
Two mark
1. Define pointer.
2. How to declare a pointer variable?
3. Give an example of pointer initialization.
4. List out the advantages of the pointer.
5. Define void pointer.
6. Define null pointer. Give example.
7. What is pointer-to-pointer? Give example program.
8. What is pointer arithmetic?
9. State the advantage of using arrays with pointers.
10. Write an example program for pointer and function.
Detail
1. Explain in detail about pointer concept used in C Language with suitable coding.
2. Explain about the various types of pointer.
3. Explain briefly about pointer and array with sample coding.
4. Explain about pointer and function with sample coding.
5. Write a C program to perform arithmetic operations such as addition, subtraction,
multiplication and division using functions and pointers

22

Вам также может понравиться