Chapter4 Array
Chapter4 Array
ARRAYS
In C programming, one of the frequently arising
problems is to handle similar types of data. For example:
If the user wants to store marks of 100 students.
This can be done by creating 100 variables individually but,
This process is rather tedious and impracticable.
This type of problem can be handled in C programming using
arrays.
An array is a sequence of data item of homogeneous
values(same type).
Arrays are of two types:
One-dimensional arrays
Multidimensional arrays
1
Chapter4_Arrays and Pointers 06/25/2024
ARRAYS
Declaration of one-dimensional array
Syntax: data_type array_name[array_size];
For example: int age[5];
Here, the name of array is age. The size of array is 5,i.e., there are 5
items(elements) of array age.
All element in an array are of the same type (int, in this case).
Array elements
Size of array defines the number of elements in an array. Each element of array can
be accessed and used by user according to the need of program.
For example: int age[5];
ARRAYS
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an
array.
ARRAY
Example of array in C programming
/* C program to find the sum marks of n students using arrays */
#include <stdio.h>
int main()
{
int marks[10], i, n, sum=0;
printf("Enter number of students: ");
scanf("%d“,&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student %d: ",i+1);
scanf("%d“,&marks[i]);
sum+=marks[i]; Output
} Enter number of students: 3
printf("Sum= %d", sum); Enter marks of student1: 12
return 0; Enter marks of student2: 31
} Enter marks of student3: 2
sum=45
4
Chapter4_Arrays and Pointers 06/25/2024
MULTIDIMENSIONAL ARRAY
C programming language allows programmer to create arrays
of arrays known as multidimensional arrays.
For example:
float a[2][6];
Here, a is an array of two dimension, which is an example of
multidimensional array.
For better understanding of multidimensional arrays, array
elements of above example can be thought of as below:
5
Chapter4_Arrays and Pointers 06/25/2024
MULTIDIMENSIONAL ARRAY
Initialization of Multidimensional Arrays
In C, multidimensional arrays can be initialized
6
Chapter4_Arrays and Pointers 06/25/2024
MULTIDIMENSIONAL ARRAY
Example of Multidimensional Array In C printf("Enter the elements of 2nd matrix\n");
Write a C program to find sum of two matrix of for(i=0;i<2;++i)
order 2*2 using multidimensional arrays where, for(j=0;j<2;++j)
elements of matrix are entered by user. {
#include <stdio.h> printf("Enter b%d%d:
int main() ",i+1,j+1); scanf("%f",&b[i]
{ [j]);
float a[2][2], b[2][2], c[2][2]; }
int i, j; for(i=0;i<2;++i)
printf("Enter the elements of 1st matrix\n"); for(j=0;j<2;++j)
/* Reading two dimensional Array with the help of {
two for loop. If there was an array of 'n' dimension, /* Writing the elements using loop. */
'n' numbers of loops are needed for inserting data
to array.*/ c[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;++i) /* Sum of corresponding elements of */
for(j=0;j<2;++j)
{ }
printf("Enter a%d%d: ",i+1,j+1); printf("\nSum Of Matrix:");
scanf("%f",&a[i][j]); for(i=0;i<2;++i)
} for(j=0;j<2;++j)
{
printf("%.1f\t“, c[i][j]);
if(j==1) /* To display sum in order. *
printf("\n");
7
}
Chapter4_Arrays and Pointers 06/25/2024
MULTIDIMENSIONAL ARRAY
Ouput
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
8
Chapter4_Arrays and Pointers 06/25/2024
POINTERS
Pointers are the powerful feature of C and (C++) programming, which differs it
from other popular programming languages like: java and Visual Basic.
Pointers are used in C program to access the memory and manipulate the
address.
Reference operator(&)
If var is a variable then, &var is the address in memory.
/* Example to demonstrate use of reference operator in C programming. */
#include <stdio.h> Output
int main() Value: 5
Address: 2686778
{ Note: You may obtain different value
of address while using this code.In
int var=5; above source code, value 5 is stored in
printf("Value: %d\n",var); the memory location 2686778. var is
just the name given to that location.
printf("Address: %d",&var); You, have already used reference
/*Notice, the ampersand(&) before var. */ operator in C program while using
scanf() function.
return 0; scanf("%d",&var);
} 12
Chapter4_Arrays and Pointers 06/25/2024
POINTER
Dereference operator(*) and Pointer variables
Pointer variable or simply pointer are the special types of
variable
data_type* pointer_variable_name;
int* p;
Above statement defines, p as pointer variable of type int.
13
Chapter4_Arrays and Pointers 06/25/2024
POINTER
/* Source code to demonstrate, handling of Output
pointers in C program */
1. #include <stdio.h> Address of c: 2686784
2. int main() Value of c: 22
3. { Address of pointer pc: 2686784
4. int* pc, c; Content of pointer pc: 22
5. c=22; Address of pointer pc: 2686784
6. printf("Address of c:%d\n", &c); Content of pointer pc: 11
7. printf("Value of c:%d\n\n", c); Address of c: 2686784
8. pc=&c; Value of c: 2
9. printf("Address of pointer pc:%d\n", pc);
10.printf("Content of pointer pc:%d\n\n", *pc);
11 .c=11;
12. printf("Address of pointer pc:%d\n", pc);
13.printf("Content of pointer pc:%d\n\n",*pc);
14.*pc=2;
15. printf("Address of c:%d\n", &c);
16. printf("Value of c:%d\n\n", c);
17. return 0;
}
14
Chapter4_Arrays and Pointers 06/25/2024
POINTER
Explanation of program and figure
Code int* pc; creates a pointer pc and a code int c; creates normal variable c. Pointer pc points
to some address and that address has garbage value. Similarly, variable c also has garbage value
at this point.
Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the memory location of
variable c.
Code pc=&c; makes pointer, point to address of c. Note that, &c is the address of variable
c (because c is normal variable) and pc is the address of pc (because pc is the pointer variable).
Since the address of pc and address of c is same, *pc will be equal to the value of c.
Code c=11; makes the value of c, 11. Since, pointer pc is pointing to address of c. Value inside
address pc will also be 11.
Code *pc=2; change the contents of the memory location pointed by pointer pc to change to 2.
Since address of pointer pc is same as address of c, value of c also changes to 2.
Commonly done mistakes in pointers
Suppose, the programmer wants pointer pc to point to the address of c. Then,
int c, *pc;
pc=c; /* pc is address whereas, c is not an address. */
*pc=&c; /* &c is address whereas, *pc is not an address. */
In both cases, pointer pc is not pointing to the address of c.
15
Chapter4_Arrays and Pointers 06/25/2024
In arrays of C programming, name of the array always points to the first element of
an array. Here, address of first element of an array is &arr[0]. Also, arr represents
the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to arr.
Also, value inside the address &arr[0] and address arr are equal.
Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] is
equivalent to *arr.
Similarly,
&arr[1] is equivalent to (arr+1) AND, arr [1] is equivalent to *(arr+1).
&arr [2] is equivalent to (arr+2) AND, arr [2] is equivalent to *(arr+2).
&arr [3] is equivalent to (arr+1) AND, arr [3] is equivalent to *(arr+3).
.
.
&arr [i] is equivalent to (arr+i) AND, arr [i] is equivalent to *(arr+i).
17
Chapter4_Arrays and Pointers 06/25/2024
programming.
20
Chapter4_Arrays and Pointers 06/25/2024
POINTERS TO POINTERS
Pointer, we know is a variable that contains address of another variable. Now this variable itself
might be another pointer. Thus, we now have a pointer that contains another pointer’s address. The
following example should make this point clear.
int main( )
{
int i = 3, *j, **k ; The output of the above program would
j = &i ; be:
k = &j ; Address of i = 65524
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ; Address of i = 65524
printf ( "\nAddress of i = %u", *k ) ;
Address of i = 65524
Address of j = 65522
printf ( "\nAddress of j = %u", &j ) ;
Address of j = 65522
printf ( "\nAddress of j = %u", k ) ; Address of k = 65520
printf ( "\nAddress of k = %u", &k ) ; Value of j = 65524
printf ( "\nValue of j = %u", j ) ; Value of k = 65522
printf ( "\nValue of k = %u", k ) ; Value of i = 3
printf ( "\nValue of i = %d", i ) ; Value of i = 3
printf ( "\nValue of i = %d", * ( &i ) ) ; Value of i = 3
printf ( "\nValue of i = %d", *j ) ; Value of i = 3
printf ( "\nValue of i = %d", **k ) ; 21
Chapter4_Arrays and Pointers 06/25/2024
required.
Although, C language inherently does not have any technique to allocated memory
dynamically, there are 4 library functions under "stdlib.h" for dynamic memory
allocation.
Function Use of function
malloc() Allocates requested size of bytes and returns a pointer first byte of
allocated space
calloc() Allocates space for an array elements, initializes to zero and then returns
a pointer to memory
free() dellocate the previously allocated space
realloc() Change the size of previously allocated space
22
Chapter4_Arrays and Pointers 06/25/2024
malloc()
The name malloc stands for "memory allocation".
and return a pointer of type void which can be casted into pointer of
any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a
ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4
bytes respectively and the pointer points to the address of first byte of
memory.
23
Chapter4_Arrays and Pointers 06/25/2024
25
Chapter4_Arrays and Pointers 06/25/2024
26
Chapter4_Arrays and Pointers 06/25/2024
27
Chapter4_Arrays and Pointers 06/25/2024
28
Chapter4_Arrays and Pointers 06/25/2024
STRING
In C programming, array of character are called strings.
A string is terminated by null character \0. For example:
Declaration of strings
Strings are declared in C in similar manner as arrays.
char s[5];
STRING
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd"; OR
char c[5]="abcd“; OR,
char c[]={'a‘,'b‘,'c‘,'d','\0'};OR
char c[5]={'a‘,'b‘,'c‘,'d','\0'};
String can also be initialized using pointers
char *c="abcd";
Reading Strings from user.
1) Reading words from user.
char c[20];
scanf("%s“, c);
String variable c can only take a word. It is because when white space is
encountered, the scanf() function terminates.
30
Chapter4_Arrays and Pointers 06/25/2024
STRING
Write a C program to illustrate how to read string from terminal.
#include<stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s“, name);
printf("Your name is %s”, name);
return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Here, program will ignore Ritchie because, scanf() function takes only
string before the white space. 31
Chapter4_Arrays and Pointers 06/25/2024
STRING
2) Reading a line of text
C program to read line of text manually.
#include <stdio.h>
int main()
{
char name[30],ch;
int i=0;
printf("Enter name: ");
while(ch!='\n') /*terminates if user hit enter */
{
ch=getchar();
name[i]=ch;
i++;
}
name[i]='\0'; /*inserting null character at end */
printf("Name: %s",name);
return 0;
}
This process of taking a string is so tedious. There are predefined functions gets() and puts in C
language to read and display string respectively. 32
Chapter4_Arrays and Pointers 06/25/2024
STRING
#include <stdio.h>
#int main()
{
char name[30];
printf("Enter name: ");
gets(name); /*Function to read string from user.*/ printf("Name: ");
puts(name); /*Function to display string.*/
return 0;
}
Both, the above program has same output below:
Output
Enter name: Tom Hanks
Name:Tom Hanks
33
Chapter4_Arrays and Pointers 06/25/2024
STRING
Passing Strings to Functions
String can be passed to function in similar manner as arrays as, string is also an array.
#include<stdio.h>
void Display(char ch[]);
int main()
{
char c[50];
printf("Enter string: ");
gets(c);
Display(c); /* Passing string c to function.*/
return 0;
}
void Display(char ch[])
{
printf("String Output: ");
puts(ch);
} Here, string c is passed from main() function to user-defined function Display(). In function
declaration, ch[] is the formal argument.
34
Chapter4_Arrays and Pointers 06/25/2024
STRINGS
String handling functions
You can perform different type of string operations manually
functions. 35
Chapter4_Arrays and Pointers 06/25/2024
STRINGS
36
Chapter4_Arrays and Pointers 06/25/2024
EXAMPLES
C Program to Find the Length of a String
You can use standard library function strlen() to find the length of a string but,
this
program computes the length of a string manually without using strlen()
funtion. #include <stdio.h>
#include <stdio.h> #include <string.h>
int main() int main()
{
{ char s[1000], i;
char s[1000], i; printf("Enter a string: ");
n( ) scanf("%s", s);
printf("Enter a string: "); t r l e
s
scanf("%s", s); s i ng i=strlen(s);
U printf("Length of string: %d",
for(i=0; s[i]!='\0'; ++i); i);
printf("Length of string: %d", i); return 0;
return 0; }
}
This program asks user to enter a string and computes the length of string
manually using for loop. 37
Chapter4_Arrays and Pointers 06/25/2024