Unit 1_Recap of C 08-09-2021
UNIT 1
RECAP
DR. NEEPA SHAH 1
SYLLABUS CONTENT
Recap of C:
Structures
Pointers
Pointers and Array
Pointers and Structures
Recursion
DR. NEEPA SHAH 2
Dr. Neepa Shah 1
Unit 1_Recap of C 08-09-2021
BASIC DATA TYPES
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Signed, Unsigned
Long, short
DR. NEEPA SHAH 3
OVERVIEW OF C
Array
Function
Recursion
Structure
Pointer
DR. NEEPA SHAH 4
Dr. Neepa Shah 2
Unit 1_Recap of C 08-09-2021
OVERVIEW OF C: ARRAYS
Need
An array is a collection of individual data elements that is ordered, fixed in size, and homogeneous.
An array is considered to be a derived data type.
Array enables the storing and manipulation of potentially huge quantities of data.
Basics:
When defining an array in a program three things need to be specified.
the type of data it can hold, i.e., int, char, double, float, etc.
The number of values it can hold, i.e., the maximum number of elements it can hold
A name
data_type array_name [SIZE];
int data[100];
DR. NEEPA SHAH 5
INITIALIZING ARRAYS
Array initialization statements as shown.
(a) int A[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
9 8 7 6 5 4 3 2 1 0 ¨ values stored in array elements
0 1 2 3 4 5 6 7 8 9 ¨index values of array elements
(b) double a[5] = {3.67, 1.21, 5.87, 7.45, 9.12}
Automatic sizing While initializing, the size of a one dimensional array can be omitted
as shown.
int arr[] = {3,1,5,7,9};
Here, the C compiler will deduce the size of the array from the initialization statement.
DR. NEEPA SHAH 6
Dr. Neepa Shah 3
Unit 1_Recap of C 08-09-2021
MULTIDIMENSIONAL ARRAYS
Arrays with more than one dimension are called multidimensional arrays.
An array of two dimensions can be declared as follows:
data_type array_name[size1][size2];
A three-dimensional array, such as a cube, can be declared as follows:
data_type array_name[size1][size2][size3]
For example, x[i] refers to an element of a one-dimensional array, x. Similarly, y[i][j] refers to
an element of a two-dimensional array, y:
DECLARE: int y[3][5]
INITIALIZE: int y[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
DR. NEEPA SHAH 7
OPERATIONS ON AN ARRAY
Insert
Delete
Update
Search
Sort
Display / traverse
Merge
Split
DR. NEEPA SHAH 8
Dr. Neepa Shah 4
Unit 1_Recap of C 08-09-2021
OVERVIEW OF C: FUNCTION
Function Declaration or Prototype
Function Definition
Function Call
Function Parameters
Function return type Function Name Parameter List
type function_name ( param1,…)
DR. NEEPA SHAH 9
FUNCTION BASICS
Function Prototype:
[return_type] function_name ( arg-type name-1,… , arg-type name-n);
Function Definition:
[return_type] funtion_name ( arg-type name-1,…, arg-type name-n)
{
statements;
}
int func1(…) /* Returns an type int. */
float func2(…) /* Returns a type float */
void finc3(…) /* Returns nothing */
DR. NEEPA SHAH 10
Dr. Neepa Shah 5
Unit 1_Recap of C 08-09-2021
FUNCTION INVOCATION
DR. NEEPA SHAH 11
FUNCTION INVOCATION CONTD.
• Program Stack
• Program Counter
• Stack Frame
• Stack Frame is pushed into stack.
• Sub-routine instructions are executed.
• Stack Frame is popped from the stack.
• Now Program Counter is holding the return
address.
DR. NEEPA SHAH 12
Dr. Neepa Shah 6
Unit 1_Recap of C 08-09-2021
FUNCTION EXAMPLES CONTD.
Without parameter (s)
With parameter (s)
Without return value
With return value
Example: Min / Max, Linear Search
DR. NEEPA SHAH 13
PARAMETER PASSING
Call by value / Pass by Value
Pass by Reference / Address
DR. NEEPA SHAH 14
Dr. Neepa Shah 7
Unit 1_Recap of C 08-09-2021
RECURSION
x! = x * (x –1) * (x – 2) *(x – 3)* … * (2) * 1
OR
x! = x * ( x – 1 )!
( x – 1 )! = ( x –1 ) * ( x – 2 )!
DR. NEEPA SHAH 15
RECURSION INVOCATION
DR. NEEPA SHAH 16
Dr. Neepa Shah 8
Unit 1_Recap of C 08-09-2021
RECURSION CONTD.
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
DR. NEEPA SHAH 17
RECURSION CONTD.
DR. NEEPA SHAH 18
Dr. Neepa Shah 9
Unit 1_Recap of C 08-09-2021
RECURSION
factorial(5) returns 5 * factorial(4)
factorial(4) returns 4 * factorial(3) factorial(5) returns 5 * factorial(4) = 5 * 24 = 120
factorial(3) returns 3 * factorial(2)
factorial(4) returns 4 * factorial(3) = 4 * 6 = 24
factorial(2) returns 2 * factorial(1)
factorial(3) returns 3 * factorial(2) = 2 so 3 * 2 = 6
factorial(1) returns 1 * factorial(0)
factorial(2) returns 2 * factorial(1) = 1 so 2 * 1 = 2
factorial(0) returns 1
factorial(1) returns 1 * factorial(0) = 1 so 1 * 1 = 1
factorial(0) returns 1
DR. NEEPA SHAH 19
DEFINING AND DECLARING STRUCTURE
struct coord
{
int x;
int y;
};
struct coord
{
int x;
int y;
} first, second;
struct coord
{
int x;
int y;
};
struct coord first, second;
DR. NEEPA SHAH 20
Dr. Neepa Shah 10
Unit 1_Recap of C 08-09-2021
ACCESSING STRUCTURE MEMBERS
Structure member operator (.) / dot operator / membership operator
first.x = 50;
first.y = 100;
printf(“%d %d”, second.x , second.y);
DR. NEEPA SHAH 21
COPY STRUCTURE
first = second;
is equivalent to,
first.x = second.x;
first.y = second.y;
DR. NEEPA SHAH 22
Dr. Neepa Shah 11
Unit 1_Recap of C 08-09-2021
STRUCTURES THAT CONTAIN STRUCTURES
struct rectangle
{
struct coord topleft;
struct coord bottomrt;
};
struct rectangle mybox;
mybox.topleft.x = 0;
mybox.topleft.y = 10;
mybox.bottomrt.x = 100;
mybox.bottomrt.y = 200;
DR. NEEPA SHAH 23
STRUCTURES THAT CONTAIN ARRAYS
struct data
{
int x[4];
char y[10];
}record;
record.x[2] = 100;
DR. NEEPA SHAH 24
Dr. Neepa Shah 12
Unit 1_Recap of C 08-09-2021
ARRAY OF STRUCTURES
struct entry
{
char fname[10];
char lname[12];
char phone[8];
};
struct entry list[100];
DR. NEEPA SHAH 25
OVERVIEW OF C: STRUCTURE CONTD.
Accessing structure elements / members
Structure within structure
Structure containing array
Array of structures
Structure and Pointer
Structure and Function (Passing structure variable, Passing address of a structure variable)
Example: Array of Structures, Structure and function
DR. NEEPA SHAH 26
Dr. Neepa Shah 13
Unit 1_Recap of C 08-09-2021
POINTER
Suppose we assign the address of xyz to a variable p.
p is said to point to the variable xyz.
Variable Value Address
xyz 50 1380 p = &xyz;
p 1380 2545
2545 1380 1380 50
DR. NEEPA SHAH
p xyz 27
OVERVIEW OF C: POINTER
#include <stdio.h>
void main( )
{
int i = 5 ;
int * j ; // Declaration of Pointer Variable
j = &i ; // Assigning address of variable i to variable j
printf("\n Address of i = %p", &i) ;
printf("\n Address of i = %p", j) ;
printf("\n Value of i = %d", i) ;
printf("\n Value of i = %d", *(&i)) ;
printf("\n Value of i = %d", *j) ;
}
DR. NEEPA SHAH 28
Dr. Neepa Shah 14
Unit 1_Recap of C 08-09-2021
USING POINTERS
int i1;
int i2; 0x1014 … 0x1000
int *ptr1;
0x1010 ptr2:
int *ptr2;
0x100C … 0x1000
i1 = 1; 0x1008 ptr1:
i2 = 2;
0x1004 i2: 2
3
ptr1 = &i1; 0x1000 i1: 3
1
ptr2 = ptr1;
*ptr1 = 3;
i2 = *ptr2;
DR. NEEPA SHAH 29
POINTER ARITHMETIC
pointer + number OR pointer – number
E.g., pointer + 1 adds 1 something to a pointer
char *p; int *p;
char a; int a;
char b; int b;
p = &a; p = &a;
p += 1; In each, p now points to b p += 1;
Adds 1*sizeof(char) to Adds 1*sizeof(int) to
the memory address the memory address
Pointer arithmetic should be used cautiously
DR. NEEPA SHAH 30
Dr. Neepa Shah 15
Unit 1_Recap of C 08-09-2021
SCALE FACTOR
Data Type Scale Factor
char 1
int 4
float 4
double 8
If p1 is an integer pointer, then
p1++
will increment the value of p1 by 4.
DR. NEEPA SHAH 31
EXAMPLE: PASSING ARGUMENTS BY VALUE
void swap(int, int);
int main()
{
a and b do not
int a, b;
Output
a=5;
b = 20 ;
swap
swap (a, b) ; a = 5, b = 20
printf ("\n a = %d, b = %d", a, b);
return 0;
}
void swap(int x, int y)
{
int t ;
t=x;
x=y;
x and y swap
y=t;
}
DR. NEEPA SHAH 32
Dr. Neepa Shah 16
Unit 1_Recap of C 08-09-2021
EXAMPLE: PASSING ARGUMENTS BY REFERENCE
void swap(int*, int*);
int main()
{
int a, b; *(&a) and *(&b) Output
a=5;
swap
b = 20 ;
swap (&a, &b) ;
a = 20, b = 5
printf ("\n a = %d, b = %d", a, b);
return 0;
}
void swap (int *x, int *y)
{
int t ;
t = *x ;
*x = *y ;
}
*y = t ;
*x and *y swap
DR. NEEPA SHAH 33
ARRAY, FUNCTION AND POINTER
#include <stdio.h>
void display( int*, int ) ;
void main( )
{
int number [ ] = { 11, 22, 33, 44, 55 } ;
display (number, 5);
}
void display (int *j , int n)
{
for (int i = 0; i <= n-1 ; i++)
{
printf("\n Element : %d", *j) ;
j++; //increment pointer to point to next location
}
}
DR. NEEPA SHAH 34
Dr. Neepa Shah 17
Unit 1_Recap of C 08-09-2021
STRUCTURE, FUNCTION AND POINTER
Example: Structure, Function and Pointer
DR. NEEPA SHAH 35
LAB 1
Write a Program for Inventory Updating using a structure having (char name[20], float rate, int qty, float price).
1. Ask the user to entered one record (keep price blank). Create a function to calculate the price=rate * qty and store it
for the record. Also, ask the user for updating the rate and qty and update the record and recalculate the price. Display
all the details of the record.
2. Rewrite First Program to make a single structure variable and a single pointer to structure. Assign the address of the
structure variable to the pointer and enter one record through the pointer and Update the record using Pointer.
3. Rewrite the First Program for Inventory Updating; this time creating an array of structures so that n records can be
kept. Ask the user to enter number of records and then details of all the records. Finally for updating, ask the user to
enter the record number [array subscript] to be updated with the new rate and qty and recalculate the price and
display either all records or only the updated record.
DR. NEEPA SHAH 36
Dr. Neepa Shah 18