KEMBAR78
Unit-2, Bca-301 Data Structures Using C | PDF | Pointer (Computer Programming) | Parameter (Computer Programming)
0% found this document useful (0 votes)
43 views12 pages

Unit-2, Bca-301 Data Structures Using C

Uploaded by

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

Unit-2, Bca-301 Data Structures Using C

Uploaded by

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

BY :- MR.

ANKIT KUMAR BALIYAN


Ankitbaliyan042@gmail.com
BCA DEPARTMENT

DATA STRUCTURES USING C


(According to Swami Vivekanand Subharti University Syllabus)

BCA 301

By :
Mr. Ankit Kumar
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

UNIT -2

II Pointers, Functions, Recursion, Structures & Unions, Operations on bits, File Handling & C Preprocessor.

Pointers
In C programming, a pointer is a variable that is used to store the address of another variable. This
variable can be of type int, char, array, function, or any other pointer. It enables us to manipulate the
data that is stored at the specific memory location without using its variables. The size of the pointer
depends on the architecture. However, in a 32-bit architecture, the size of a pointer is 2 bytes. The
symbol of an address is represented by a pointer (*) asterisk symbol.

Syntax
It has the following syntax:

data_type *var_name;

In this syntax,

o data_type: It represents the data type that the pointer is pointing to.
o var_name: It represents the pointer name.
Consider the following example to define a pointer which stores the address of an integer.

int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.

Declaring a pointer
The pointer in the C language can be declared using * (asterisk symbol). It is also known as an
indirection pointer used to dereference a pointer.

int *a; //pointer to int


char *c; //pointer to char
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Example

#include<stdio.h>
int main(){ //using main function
int number=50;
int *p;
p=&number; //stores the address of number variable
printf("Address of p variable is %x \n", p);
printf("Value of p variable is %d \n", *p);
return 0;
}

Output:

Address of p variable is d86801c


Value of p variable is 50

Pointer to array
In C programming, a pointer to an array is a type of pointer that is used to store the address of the first
array element. It can be utilized to access and manipulate the array elements using the pointer arithmetic.

C Pointer to an Array Example


Let us take an example to illustrate a pointer to an array in C.

Example

#include <stdio.h>
int main() { //main function
int arr[5] = {10, 20, 30, 40, 50};
int (*ptr)[5] = &arr;
printf("First element: %d\n", (*ptr)[0]);
printf("Second element: %d\n", (*ptr)[1]);
return 0;
}

Output:

First element: 10
Second element: 20
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Addressof (&) Operator

The address of operator '&' returns the address of a variable. However, we need to use %u to display
the address of a variable.

C Addressof (&) Operator Example


Let us take an example to illustrate the addressof (&) operator in C.

Example

#include<stdio.h>
int main(){ //main function
int number=50;
printf("value of number is %d, address of number is %u",number,&number);
return 0;
}

Output:

value of number is 50, address of number is 2617052868

Types of Pointers
1) NULL Pointer
In C programming, a pointer that is not assigned any value but NULL is known as the NULL pointer.
If we don't have any address to be specified in the pointer at the time of declaration, we can assign a
NULL value. It will provide a better approach.

Syntax

It has the following syntax:

int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).

2) Void Pointer
In C programming, void pointers are the type of pointers that don't contain any data type associated
with them. They are also referred to as generic pointers because they can be used to point at any type.

C Void Pointer Example

Let us take an example to illustrate the void pointer in C.


BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Example

#include <stdio.h>
int main() { //main function
int a = 21;
float b = 3.74;
void *ptr;
ptr = &a;
printf("Value of a using void pointer: %d\n", *(int *)ptr);
ptr = &b;
printf("Value of b using void pointer: %.2f\n", *(float *)ptr);
return 0;
}

Output:

Value of a using void pointer: 21


Value of b using void pointer: 3.74
Advantage of Pointer
There are several advantage of pointer in C programming. Some of them are as follows:

o In C programming, a pointer reduces the code and improves the performance. It is used to
retrieve strings, trees, etc. and used with arrays, structures, and functions.
o We can return multiple values from a function using the pointer.
o It allows us to access any memory location in the computer's memory.

Pointer Program to swap two numbers without using the 3rd Variable
Let us take an example to swap two numbers without using the 3rd variable in C.
Example
#include<stdio.h>
int main()
{ //main function
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
return 0;
}

Output:

Before swap: *p1=10 *p2=20


After swap: *p1=20 *p2=10
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Usage of pointer
There are many applications of pointers in C language. Some of them are as follows:

o Dynamic Memory Allocation: In C language, we can dynamically allocate memory using


malloc() and calloc() functions where the pointer is used.
o Arrays, Functions, and Structures: Pointers are widely used in arrays, functions, and
structures. It reduces the code and improves the performance.
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Func ons
In C programming, a function is a block of code that can perform a specific task. It consists of a set of
programming statements that is enclosed by curly braces {}. A function can be called multiple times to
provide reusability and modularity to the C program. In other words, we can say that the collection of
functions creates a program. The function is also known as procedure or subroutine in other
programming languages.

Definition and Declaration of Function in C


Here, we will see how we can define and declare the functions in C.

Function Declaration
In C programming, a function declaration gives details about a function's name, parameters, and
return type, allowing the compiler to make use of the information before it is defined. It ensures that
function calls are valid and type-safe even if the function is defined later in the program.

Syntax of Function Declaration

It has the following syntax:

return_type function_name(parameter_list);

In this syntax;

o return_type: It represents the return type, such as int, char, double, string, void, etc.
o function_name: It is used to represent the name of the function.
o parameter_list: It is used to represent the name of the parameter.
Function Declaration Example
Let us take an example to illustrate the function declaration in C.

int add(int a, int b); // Declaring a function that returns an int and takes two int arguments
Function Definition
In C programming, a function definition provides the actual body of the function, which means the
block of code that performs a specific task. It informs the compiler what the function does when it is
called. Unlike a function declaration, the definition includes the implementation.

int add(int a, int b)


{
return a + b;
}
Function Call
In C programming, a function call is a way of using a function by giving its name and necessary
arguments. When the function is called, the control goes to the code that has been written within the
definition of that function, and then the output is returned. It enables code reuse and modular
programming in C, resulting in better organization.
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Syntax for Function Call

It has the following syntax:

function_name(actual_arguments);
Function Call Example in C

Let us take an example to illustrate how a function calls in C.

int result = add(5, 10); // Calling the function and storing result
Simple Function Example in C
Let us take an example to illustrate function in C.

Example

#include <stdio.h>

// Function Declaration
int multiply(int, int);

int main()
{ //main function
int result;

// Calling the add function


result = multiply(4, 5);

printf("The Multiplication of Numbers is: %d\n", result);


return 0;
}

// Function Definition
int multiply(int a, int b)
{
return a * b;
}
Output:

The Multiplication of Numbers is: 20


BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Recursion

In the C programming language, recursion is the process where a function calls itself repeatedly,
directly or indirectly, until a specific base condition is met. Recursion involves several numbers of
recursive calls. Any function which calls itself is called a recursive function. Recursion cannot be
applied to all problems, but it is more useful for tasks that can be defined in terms of similar
subtasks. For Example, recursion may be applied to sorting, searching, and traversal problems.

Syntax of Recursion in C
It has the following syntax:

return_type function_name(parameters) {
if (base_condition) {
// stop recursion
return result;
} else {
// recursive call
return function_name(modified_parameters);
}
}
In this syntax,

o return_type: It represents the data type that the function returns.


o function_name: It represents the name of the function.
o base_condition: It is a condition that is used to terminate the recursion.
o Recursive condition: It is the condition where the function calls itself with modified
arguments, which is regularly approach the base case.
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Factorial Example in C using Recursion


Let us take an example to illustrate how to find factorial using recursion in C.

Example
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}

Output:
Enter the number whose factorial you want to calculate?5
factorial = 120
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Example to find nth term of Fibonacci Series using recursion

#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n: ");
scanf("%d",&n);
f = fibonacci(n);
printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}

Output:

Enter the value of n: 12


144
BY :- MR. ANKIT KUMAR BALIYAN
Ankitbaliyan042@gmail.com
BCA DEPARTMENT

Advantages and Disadvantages of Recursion


There are several advantages and disadvantages of Recursion in C. Some main advantages
and disadvantages are as follows:

Advantages:

o Recursion helps to make easy code writing.


o It reduces the number of unnecessary function calls.
o It is very useful when the same solution is being used.
o It reduces the length of code.
o It is very helpful in solving the data structure problem.
o It helps to evaluate Infix, prefix, and postfix stack, etc.

Disadvantages:

o In general, recursive functions are slower than their non-recursive counterparts.


o A lot of memory may be required to store intermediate results on the system stacks.
o The code is hard to read or understand.
o It is not more efficient in terms of both time and space complexity.
o If the recursive calls are not properly checked, the machine can exhaust its memory.

You might also like