C Unit Iii
C Unit Iii
UNIT - III
Arrays: Array notation and representation, Declaring one-
dimensional array, Initializing arrays, Accessing array elements,
Manipulating array elements, Arrays of unknown or varying size,
Two-dimensional arrays, Multidimensional arrays.
PROBLEM SOLVING USING C Pointers: Introduction, Characteristics, * and & operators, Pointer
type declaration and assignment, Pointer arithmetic, Call by
KCA102 reference, Passing pointers to functions, array of pointers, Pointers
UNIT-III to functions, Pointer to pointer, Array of pointers.
MCA I YEAR, I SEM. Strings: Introduction, Initializing strings, Accessing string elements,
Array of strings, Passing strings to functions, String functions.
UNITED INSTITUTE OF MANAGEMENT, NAINI MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
1
01/11/2022
What is an Array?
Example of an Array:
So far we have used only single variable name for storing one
data item. If we need to store multiple copies of the same data
then it is very difficult for the user. To overcome the difficulty a Suppose we have to store the roll numbers of the 100
new data structure is used called arrays. students the we have to declare 100 variables named
as roll1, roll2, roll3, ……. roll100 which is very difficult
An array is a linear and homogeneous data structure. job. Concept of C programming arrays is introduced in
An array permits homogeneous data. It means that similar C which gives the capability to store the 100 roll
types of elements are stored contiguously in the memory numbers in the contiguous memory which has 100
under one variable name. blocks and which can be accessed by single variable
An array can be declared of any standard or custom data name.
type.
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
2
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
3
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
4
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
5
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
6
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
7
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
8
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
9
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
10
01/11/2022
5. Two-dimensional arrays are used to represent matrices. 2- An array is a static structure (which means the array is of
fixed size). Once declared the size of the array cannot be
modified. The memory which is allocated to it cannot be
increased or decreased.
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
11
01/11/2022
4- Allocating more memory than the requirement leads to 1- Arrays can be used for CPU scheduling.
wastage of memory space and less allocation of memory also
leads to a problem. 2-Arrays can be used for performing matrix
operations. Many databases, small and large, consist of one-
dimensional and two-dimensional arrays whose elements
are records.
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
12
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
13
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
14
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
15
01/11/2022
Conti…
Declaration of 2D Array in C:
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i); data_type array_name[rows][columns];
scanf("%d",&a[i]);
} int a[4][3];
for(i=0; i<n; i++)
{
sum += a[i];
}
16
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
17
01/11/2022
18
01/11/2022
Continue… Continue…
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
19
01/11/2022
20
01/11/2022
Conti… Conti…
printf("Enter second matrix:\n"); for(i=1;i<=2;i++)
for(i=1;i<=2;i++) {
{ for(j=1;j<=2;j++)
for(j=1;j<=2;j++) {
{ s[i][j]=0;
scanf("%d",&b[i][j]); for(k=1;k<=2;k++)
} {
} s[i][j] +=a[i][k]*b[k][j];
}
}
}
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
21
01/11/2022
Conti…
printf("Matrix Multiplication Is: \n");
for(i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
printf("%d ",s[i][j]); Pointer in C
}
printf("\n");
}
getch();
}
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
22
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
23
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
24
01/11/2022
Example- Note - %d or %u
%d – 32768 to 32767
void main() %u – 0 to 65535
{
int x=5; Output -
printf(“%d\n”, x); 5
printf(“%d\n”, &x); 2048
printf(“%d\n”, *&x); 5
}
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
25
01/11/2022
Question-? But…
int x = 5; int x = 5; x j
x
&x = 7; int *j;
j = &x;
5 2048
5
2048 3000
2048
We can’t store anything in &x as &x is not a variable, it is We can store address in another variable.
the way to represent address of block x. But j has to be declared before use.
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
26
01/11/2022
But… Pointer-
27
01/11/2022
UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 3 | C PROGRAMMING MR. ASHUTOSH PANDEY
28
01/11/2022
29
01/11/2022
Example-Call by value in C: Call by Value Example: Swapping the values of the two variables
30
01/11/2022
void swap (int a, int b) In call by reference, the address of the variable is
{ passed into the function call as the actual parameter.
int temp;
The value of the actual parameters can be modified
temp = a;
a=b; by changing the formal parameters since the address
b=temp; of the actual parameters is passed.
printf("After swapping values in function a = %d, b = %d\n",a,b); In call by reference, the memory allocation is similar
// Formal parameters, a = 20, b = 10 for both formal parameters and actual parameters. All
} the operations in the function are performed on the
Output -
value stored at the address of the actual parameters,
Before swapping the values in main a = 10, b = 20 and the modified value gets stored at the same
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20 address.
31
01/11/2022
32
01/11/2022
33
01/11/2022
Output
elements at the array are : 10 20 30
34
01/11/2022
Output
a=10
a value through pointer = 10
a value through pointer to pointer = 10
35
01/11/2022
The string can be defined as the one-dimensional There are two ways to declare a string in c language.
array of characters terminated by a null ('\0').
The character array or the string is used to manipulate
text such as word or sentences. 1. By char array
Each character in the array occupies one byte of
2. By string literal
memory, and the last character must always be 0.
The termination character ('\0') is important in a
string since it is the only way to identify where the
string ends.
36
01/11/2022
2. We can also define the string by the string literal in C language. printf("Char Array Value is: %s\n", ch1);
printf("String Literal Value is: %s\n", ch2);
char ch[]=“UIM"; return 0;
}
In such case, '\0' will be appended at the end of the string
by the compiler.
37
01/11/2022
#include<stdio.h> #include<stdio.h>
void main () void main ()
{ {
char s[10] = “allahabad"; char s[11] = “allahabad";
int i = 0; int i = 0;
int count = 0; int count = 0;
while(i<10) while(s[i] != NULL)
{ {
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{ {
count ++; count ++;
} Output } Output
i++; The number of vowels :4 i++; The number of vowels :4
} }
printf("The number of vowels %d",count); printf("The number of vowels %d",count);
} }
38
01/11/2022
instructs the compiler to store the string s while the new line (\n) }
is encountered.
39
01/11/2022
puts() functions-
Example 4: Strings and Pointers
Description
The C library function int puts(const char *str) writes a string to stdout up to but not
including the null character. A newline character is appended to the output. #include <stdio.h>
Declaration void main()
int puts(char[]) {
char name[] = "Harry Potter";
printf("%c", *name);
#include<stdio.h>
printf("%c", *(name+1)); Output
#include <string.h> H
printf("%c", *(name+7));
int main(){ Output a
char name[50]; o
Enter your name: Raj
Your name is: Raj char *namePtr;
printf("Enter your name: "); H
namePtr = name;
gets(name); //reads string from user a
printf("%c", *namePtr); o
printf("Your name is: ");
printf("%c", *(namePtr+1));
puts(name); //displays string
printf("%c", *(namePtr+7));
return 0;
}
}
40
01/11/2022
%c and % s - %c and %s
Conti…
41
01/11/2022
C String Functions-
strlen()
No. Function Description
1) strlen(string_name) returns the length of string
#include<stdio.h>
name.
#include <string.h>
strcpy(destination, source) copies the contents of int main(){ Output:
2) source string to destination char ch[20]={‘a', ‘s', ‘h', ‘u', 't', ‘o', ‘s', ‘h', '\0'}; Length of string is: 8
string. printf("Length of string is: %d",strlen(ch));
strcat(first_string, concats or joins first string return 0;
3) second_string) with second string. The }
result of the string is strcpy()
stored in first string.
strcmp(first_string, compares the first string #include<stdio.h>
4) second_string) with second string. If both #include <string.h>
strings are same, it returns int main(){
0. char ch[20]={‘i', ‘n', ‘d', ‘i', ‘a', '\0'};
char ch2[20]; Output:
5) strrev(string) returns reverse string. strcpy(ch2,ch); Value of second string is: javatpoint
42
01/11/2022
43
01/11/2022
44
01/11/2022
45