Module 3 Array
Module 3 Array
2. When process try to refer a part of the memory then it will firstly refer the base
address from base register and then it will refer relative address of memory
location with respect to base address.
Array Terminologies:
Characteristics of an array:
1. The declaration int a [5] is nothing but creation of five variables of integer
types in memory instead of declaring five variables for five values.
2. All the elements of an array share the same name and they are
distinguished from one another with the help of the element number.
3. The element number in an array plays a major role for calling each element.
4. Any particular element of an array can be modified separately without
disturbing the other elements.
5. Any element of an array a[ ] can be assigned or equated to another ordinary
variable or array variable of its type.
6. Array elements are stored in contiguous memory locations.
Types of Array
1. Single Dimensional Array / One Dimensional Array
int a[3][4];
for(i=0;i<row,i++)
for(j=0;j<col,j++) {
printf("%d",a[i][j]); }
Two-Dimensional Arrays: Summary with Sample Example:
Memory Representation:
1. 2-D arrays are stored in contiguous memory location row wise.
2. 3 X 3 Array is shown below in the first Diagram.
3. Consider 3×3 Array is stored in Contiguous memory location which
starts from 4000.
4. Array element a[0][0] will be stored at address 4000 again a[0][1]
will be stored to next memory location i.e. Elements stored row-wise
5. After Elements of First Row are stored in appropriate memory
locations, elements of next row get their corresponding memory
locations.
6. This is integer array so each element requires 2 bytes of memory.
Basic Memory Address Calculation: a[0][1] = a[0][0] + Size of Data Type
Method 1: Initializing all Elements row wise
int a[3][2] = {1 , 4 , 5 , 2 , 6 , 5 };
Some Elements could be initialized
int a[3][2] = { { 1 }, { 5 , 2 }, { 6 } };
Uninitialized elements will get default 0 value. In this case we have declared and
initialized 2-D array like this
Accessing 2D Array Elements:
1. To access every 2D array we requires 2 Subscript variables.
2. i.e. For Zeroth row it will accept zeroth, first, second column (a[0][0], a[0][1],
a[0][2]) elements
3. In Next Iteration Row number will be incremented by 1 and the column number
again initialized to 0.
char s[5];
Declaring and Initializing a string variables:
#include <stdio.h>
int main () {
return 0;
}
String Initialization
Read String from the user
To get a value of string variable use the two
different types of formats.
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
use fgets() function to read a line of string.
#include <stdio.h>
#define MAX 15
int main()
{
char buf[MAX];
fgets(buf, MAX, stdin);
printf("string is: %s\n", buf);
return 0;
}
How to read a line of text?
use gets() function to read a line of string. And, use puts() to display the string.
#include <stdio.h>
#define MAX 15
void main()
{
char Arr[MAX];
printf("Enter a string: ");
gets(Arr);
printf(" entered String is");
puts(Arr);
//printf("string is: %s\n", Arr);
}
#include<stdio.h>
int main()
{
char text[100];
int i;
The main difference between fgets() function and gets() function is that
fgets() function allows the user to specify the maximum number of
characters to read
Commonly Used String Functions
return 0;
}
● strcpy() - copies a string to another
● The strcpy() function copies the string pointed by source (including the
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100];
char str2[20];
int i;