Programming in C Module 3: Arrays, Strings & Pointers Guide, Lecture notes of C programming

Subject: Programming in C (Course Code: 1BEIT105/205) Institution: Vemana Institute of Technology (Dept. of ISE) Affiliation: Visvesvaraya Technological University (VTU) Author: Prof. Chandana D.C This comprehensive study material covers Module 3 of the C Programming curriculum, providing an in-depth exploration of data structures and memory management. It is a vital resource for engineering students preparing for VTU exams. Detailed Syllabus Coverage: Arrays & Strings: Single-dimension, two-dimensional, and multidimensional arrays. Includes array initialization, variable-length arrays, and passing arrays to functions. Pointers: Fundamental pointer concepts, pointer variables, operators, expressions, and the relationship between pointers and arrays. Advanced Memory: Multiple indirection (pointers to pointers), initializing pointers, and an introduction to dynamically allocated arrays using malloc(). Key Features: The document includes theory explanation paired with practical example

Typology: Lecture notes

2025/2026

Available from 02/18/2026

mayur-anand
mayur-anand 🇮🇳

4 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAMMING IN C MODULE-3
Prof.Chandana D.C,Dept.of ISE. VEMANAIT
SUBJECT: PROGRAMMING IN C
SUBJECT: 1BEIT105/205
MODULE -3
Arrays and Strings: Single-Dimension Arrays,
Generating a Pointer to an Array, Passing Single-
Dimension Arrays to Functions, Strings, Two-
Dimensional Arrays, Multidimensional Arrays, Array
Initialization, Variable Length Arrays.
Pointers: What Are Pointers?, Pointer Variables, The
Pointer Operators, Pointer Expressions, Pointers and
Arrays, Multiple Indirection, Initializing Pointers.
Textbook 1: Chapter 4, 5
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Programming in C Module 3: Arrays, Strings & Pointers Guide and more Lecture notes C programming in PDF only on Docsity!

PROGRAMMING IN C MODULE- 3

Prof.Chandana D.C,Dept.of ISE. VEMANAIT

SUBJECT: PROGRAMMING IN C

SUBJECT: 1BEIT105/

MODULE - 3

Arrays and Strings: Single-Dimension Arrays,

Generating a Pointer to an Array, Passing Single-

Dimension Arrays to Functions, Strings, Two-

Dimensional Arrays, Multidimensional Arrays, Array

Initialization, Variable Length Arrays.

Pointers: What Are Pointers?, Pointer Variables, The

Pointer Operators, Pointer Expressions, Pointers and

Arrays, Multiple Indirection, Initializing Pointers.

Textbook 1: Chapter 4, 5

ARRAYS

1. WHY DO WE NEED ARRAYS?

Arrays are used to represent multiple data items of the same type using single name.

2. DEFINITION OF ARRAYS

 The array is a collection of homogeneous elements of same data type.  The array index starts with 0.  Arrays are called as subscripted variables because; it is accessed using subscripts/indexes.  Ex: 1. List of employees in an organization.

  1. List of products and their cost sold by a store.
  2. Test scores of a class of students.  Here marks[0]=10, marks[1]=20, marks[2]=30 and marks[3]=40. This subscript/index notation is borrowed from Mathematics where we have the practice of writing: marks4, marks 3 , marks 2 and so on. Properties of Array Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. o Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. o Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. **Advantage of C Array
  1. Code Optimization** : Less code to the access the data. 2) Ease of traversing : By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting: To sort the elements of the array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array. Disadvantage of C Array Fixed Size : Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like Linked List which we will learn later

b) Initialization without size:  In the declaration the array size will be set to the total number of initial values specified.  The compiler will set the size based on the number of initial values.  Syntax: data_type array_name[ ]={list of values}; Examples: int marks[ ]={ 95,35, 67, 87}; float temperature[ ]={29.5, 30.7, 35.6, 45.7, 19.5}; a) Partial initialization:  If we not specify the all the elements in the array, the unspecified elementswill be initialized to zero.  In such a case elements are initialized in the order from 0th element.  The remaining elements will be initialized to zero automatically by the compiler. Example : int marks[5]={10,12,20}; Here, marks[3] and marks[4] will be initialized to zero. a) Initializing all the elements zero: If we want to store zero to all the elements in the array we can do. Examples: int marks[4]={0}; e) String Initialization  Sequence of characters enclosed within double quotes is called as string.  The string always ends with NULL character (\0) Assigning values to arrays Using assignment operators, we can assign values to individual elements of arrays. For example: int a[3]; a[0]=10;

a[1]=20; a[2]=30;

2. Run time initialization: Run time initialization is storing values in an array when program is running or executing. Example: printf(“Enter 4 marks”); for(i=0; i<4; i++) { scanf(“ %d”, &marks[i]); } Reading and writing single dimensional arrays. To read array elements from keyboard we can use scanf() function as follows: To read 0th element: scanf(“%d”,&a[0]); To read 1st element: scanf(“%d”,&a[1]); To read 2nd element: scanf(“%d”,&a[2]); …… ……. To read nth element : scanf(“%d”,&a[n-1]); In general To read ith element: scanf(“%d”,&a[i]); where i=0; i Write a c program to find largest of n elements stored in an array a. #include void main() { int a[10],i,n, big; printf("Enter size of array: "); scanf("%d",&n); printf("Enter %d elements in the array : ", n); for(i=0;ibig) big=a[i]; } printf(" big %d ",big); } OUTPUT Enter size of array: 5 Enter 5 elements in the array : 12 45 7 1 7 Elements in array are: big 45 Write a C program to generate Fibonacci numbers using arrays. #include void main() { int fib[24]; int i,n; printf("Enter size of array: "); scanf("%d",&n); fib[0] = 0; fib[1] = 1; for(i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } for (i = 0; i < n; i++) { printf("%d \t", fib[i]); } } OUPUT

Write a C program to evaluate the polynomial using Horners method. #include void main() { float a[100],sum=0,x; int n,i; printf("\nEnter degree of the polynomial X :: "); scanf("%d",&n); printf("\nEnter coefficient's of the polynomial X :: \n"); for(i=n;i>=0;i--) { printf("\nEnter Coefficient of [ X^%d ] :: ",i); scanf("%f",&a[i]); } printf("\nEnter the value of X :: "); scanf("%f",&x); for(i=n;i>0;i--) { sum=(sum+a[i])*x; } sum=sum+a[0]; printf("\nValue of the polynomial is = [ %f ]\n",sum); }

OUT PUT

Enter degree of the polynomial X :: 3 Enter coefficient's of the polynomial X :: Enter Coefficient of [ X^3 ] :: 1 Enter Coefficient of [ X^2 ] :: 3 Enter Coefficient of [ X^1 ] :: 2 Enter Coefficient of [ X^0 ] :: 5 Enter the value of X :: 4 Value of the polynomial is = [ 125.000000 ]

2. Run time initialization: Run time initialization is storing values in an array when program is running or executing. Following example illustrates run time storing of values using scanf and for loop: Example: printf(“Enter the marks”); for(i=0; i<3; i++) for(j=0;j<3;j++) { scanf(“ %d”, &marks[i][j]); } More Examples: Other way of initialization: int a[ ][3]= { 0, 1, 2, 3,4,5,6,7,8}; int b[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12}; a Example for Invalid initialization int A[3][ ]={1,2,3}; Note: Never have column size undeclared in two dimension array.

III. Multi-Dimensional Array: It can have 3, 4 or more dimensions. A three dimensional array is an

array of 2D arrays. It has row, columns, depth associated with it. NOTE: USING ARRAYS WITH FUNCTIONS: In large programs that use functions we can pass Arrays as parameters. Two ways of passing arrays to functions are:

  1. Pass individual elements of array as parameter
  2. Pass complete array as parameter Pass individual elements of array as parameter Pass complete array as parameter Here, each individual element of array is passed to function separately. Here, the complete array is passed to the function. Example: #include int square(int); Example : #include int sum(int [ ]); 0 1 2 1 2 3 4 3 4 5 b^5 6 7 6 7 8 9 10 11 12

int main( ) { int num[5], i; num[5] ={1, 2, 3, 4, 5}; for(i=0; i<5; i++) { square(num[i]); } } int square(int n) { int sq; sq= n * n; printf(“%d ”, sq); } int main( ) { int marks[5], i; marks[5] ={10, 20, 30, 40, 50}; sum(marks); } int sum(int n[ ]) { int i, sum=0; for(i=0; i<5; i++) { sum = sum+n[i]; } printf(“Sum = %d ”, sum); } Write a c program to read & print 2d array as a Array. #include void main() { int m, n , i, j , a[3][3]; printf(“Enter number of Rows and Columns\n”); scanf(“%d %d”, &m, &n); printf(“Enter array Elements\n”); for(i=0;i for(j=0;j void main() { int m, n, i, j, a[3][3]; printf(“Enter Number of Rows and Columns\n”); scanf(“%d %d”, &m, &n); printf(“Enter Array Elements\n”); for(i=0; i a[i][j]) big = a[i][j]; } } printf(“Big is %”, big); }

Matrix Multiplication Programs #include #include void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q; printf("Enter the order of matrix A\n"); scanf("%d%d",&m,&n); printf("Enter the order of matrix B\n"); scanf("%d%d",&p,&q); if(n!=p) { printf("Matrix multiplication is not possible\n"); exit(0); } printf("Enter the elements of matrix A\n"); for(i = 0 ; i < m; i++) { for(j = 0 ; j < n; j++) { scanf("%d",&a[i][j]); } } printf("Enter the elements of matrix B\n"); for(i = 0 ; i < p; i++) { for(j = 0 ; j < q; j++) { scanf("%d",&b[i][j]); } } for(i=0 ; i < m ; i++) { for(j=0 ; j < q ; j++) { c[i][j] = 0; for(k=0;k if(Flag == 1) { printf("\n The Matrix that you entered is an Identity Matrix "); } else { printf("\n The Matrix that you entered is Not an Identity Matrix "); } } C program to find Transpose of a Matrix #include void main() { int a[10][10], transpose[10][10], r, c; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); // asssigning elements to the matrix printf("\nEnter matrix elements:\n"); for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } // printing the matrix a[][] printf("\nEntered matrix: \n"); for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { printf("%d ", a[i][j]); if (j == c - 1) printf("\n"); } // computing the transpose for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { transpose[j][i] = a[i][j]; } // printing the transpose printf("\nTranspose of the matrix:\n"); for (int i = 0; i < c; ++i) for (int j = 0; j < r; ++j) { printf("%d ", transpose[i][j]); if (j == r - 1) printf("\n"); } }

III SEARCHING ALGORITHMS

The process of finding a particular item in the large amount of data is called searching. The element to be searched is called key element. There are two methods of searching: 1] Linear search. 2] Binary search

1. Linear Search:

  • Linear search also called sequential search is a simple searching technique.
  • In this technique we search for a given key item in linear order i.e,one after the other from first Element to last element.
  • The search may be successful or unsuccessful.
  • If key item is present, the search is successful, otherwise unsuccessful search. Program to implement linear search. #include void main ( ) { int i, n, a[10], key; printf(“Enter Number of Elements to be stored in an array\n”); scanf(“%d”, &n); printf(“Enter Array Elements \n”);

2. Binary Search:

  • Binary search is a simple and very efficient searching technique which can be applied if the items are Arranged in either ascending or descending order.
  • In binary search first element is considered as low and last element is considered as high.
  • Position of middle element is found by taking first and last element is as follows. mid=(low+high)/
  • Mid element is compared with key element, if they are same, the search is successful.
  • Otherwise if key element is less than middle element then searching continues in left part of the array.
  • If key element is greater than middle element then searching continues in right part of the array.
  • The procedure is repeated till key item is found or key item is not found.
  • Note: Elements in the array Need to in Sorted Order otherwise Binary Search is not Going to work properly Advantages of binary search
  • Simple technique
  • Very efficient searching technique Disadvantages
  • The elements should be sorted.
  • It is necessary to obtain the middle element, which are stored in array. If the elements are stored in Linked list, this method cannot be used. Disadvantages Elements Need to be kept in sorted order before applying the Binary Search Algorithm

C program to search a key in a list of names using Binary searching technique/* #include #include void main() { int a[20],low,mid,high,i,n,k; printf("Enter the number of elements\n"); scanf("%d",&n); printf("%d\n",n); printf("Enter the elements\n"); for(i = 0 ; i < n ; i++) scanf("%d",&a[i]); printf("Enter the element to be searched\n"); scanf("%d",&k); low = 0; high = n - 1; while(low <= high) { mid = (low + high)/2; if(a[mid] == k) { printf("Successful search, element %d found at %d\n",k,mid+1); exit(0); } else if(a[mid] > k) { high = mid - 1; } else { low = mid + 1; } } printf("Unsuucessful search\n"); }