Arrays - Definition, Declaration of Array, Initialization, Storing
Arrays - Definition, Declaration of Array, Initialization, Storing
Arrays - Definition, Declaration of Array, Initialization, Storing
Arrays – Definition, declaration of array, Initialization, storing
values in array, Two dimensional arrays, Multi-dimensional
arrays. Arrays and Pointers, Array of pointers
Strings – Declaration and Initialization, String Input / Output
functions, String manipulation functions, strings and pointers,
Arrays of strings
1
Arrays
An array is defined as the collection of similar data items
stored at contiguous memory locations under the same name.
Arrays are the derived data type in C programming language
which can store the primitive type of data such as int, char,
double, float, etc.
Properties of Array
• Here int is the data type, arr is the name of the array and 10 is the
size of array. It means array arr can only contain 10 elements of int
type.
• Index of an array starts from 0 to size-1 i.e first element of arr array
will be stored at arr[0] address and the last element will occupy
arr[9].
1-D Array Initialization
It is possible to initialize an array during declaration.
Syntax: datatype array_name[size] = { val1, val2, val3, ..... valN };
Example:
int mark[5] = {19, 10, 8, 17, 9};
You can also initialize an array like this:
int mark[] = {19, 10, 8, 17, 9};
Here, we haven't specified the size. However, the compiler knows its
size is 5 as we are initializing it with 5 elements.
Accessing 1-D Arrays
Elements of an array can be accessed by indices.
The first element is mark[0], the second element is mark[1] and so on.
Few keynotes:
• Arrays have 0 as the first index, not 1. In this example, mark[0] is
the first element.
• If the size of an array is n, to access the last element, the n-1 index
is used. In this example, mark[4]
• Suppose the starting address of mark[0] is 2120. Then, the address
of the mark[1] will be 2124. Similarly, the address of mark[2] will
be 2128 and so on.
• This is because the size of a int is 4 bytes.
1-D Arrays-Example
// Program to read and print an array
#include <stdio.h>
int main()
{
int marks[10];
printf("Enter number of subjects: ");
scanf("%d", &n);
printf("Enter marks");
for(i=0; i<n; i++)
{
scanf("%d", &marks[i]);
}
for(i=0; i<n; i++)
Output
{
Enter number of subjects: 5
printf("%d", marks[i]);
Enter marks 50 60 70 80 90
}
50 60 70 80 90
return 0;
}
1-D Arrays-Example
// Program to print the array in reverse
#include <stdio.h>
int main()
{
int a[10];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=n-1; i>=0; i--)
{ Output
printf("%d", a[i]); Enter number of elements: 6
} Enter elements 5 8 9 2 1 6
return 0; 612985
}
1-D Arrays-Example
// Program to find the average of n numbers using arrays
#include<stdio.h>
int main()
{
int marks[10], i, n, sum = 0, avg; Output
printf("Enter number of subjects: "); Enter number of subjects: 5
scanf("%d", &n); Enter marks 50 60 70 80 90
printf("Enter marks: "); Sum = 350
for(i=0; i<n; i++) Average = 70.00
{
scanf("%d", &marks[i]);
}
for(i=0; i<n; i++) avg=sum/n;
{ Printf(“sum=%d”,sum);
sum=sum+marks[i]; printf("Average = %d", avg);
} return 0;
}
//program to find largest and smallest number in an array
int main()
{
int a[50],i,n,large,small;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); Output
} Enter the number of elements: 6
large = small =a[0]; Enter elements: 40 60 20 80 90 50
for(i=1;i<n;i++) largest number is 90
{
if(a[i]>large)
smallest number is 20
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("largest number is %d \n",large);
printf("smallest number is %d",small); return 0;
}
2-D Arrays
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as the
collection of rows and columns.
22
12
34
22
12
34
Multi – Dimensional Array
char str[10];
char str[10]=“Hello”;
Strings-Declaring and Initializing
There are different ways to initialize a character array variable.
#include<stdio.h>
int main( )
{
char str[15] = "hello mrcet";
char str1[15] ={'h','o','w',' ','a','r','e',' ','y','o','u'};
printf("%s %s",str,str1);
return 0;
}
Output
hello mrcet how are you
Strings-Example Programs
//program to read and print city
#include<stdio.h> #include<stdio.h>
int main() #include <string.h>
{ int main()
char city[50]; {
printf("Enter your city: "); char city[50];
scanf(“%s”,city); printf("Enter your city: ");
printf("Your city is: "); gets(city);
printf(“%s”,city); printf("Your city is: ");
return 0; puts(city);
} return 0;
}
Output
Enter your city: Delhi
Your city is: Delhi
Output
Enter your city: New Delhi
Your city is: New Delhi
Enter your city: New Delhi
Your city is: New
getchar() and putchar()
getchar() function reads the next available character from the screen and returns it as an
integer. This function reads only single character at a time. You can use this method in the
loop in case you want to read more than one character from the screen.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
printf("Length of string a = %d \n",strlen(a));
printf("Length of string b = %d \n",strlen(b));
return 0;
}
Output:
Length of string a = 7
Length of string b = 7
Example:
#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
int length;
printf("\n\nEnter a string you wish to calculate the length of : ");
gets(a);
length = strlen(a);
printf("\n\nThe length of the input string is: %d\n\n", length);
return 0;
}
OUTPUT:
Enter a string you wish to calculate the length of : mrcet
The length of the input string is: 5
Strcpy()
The strcpy() function copies the string pointed by source to the destination.
The strcpy() function also returns the copied string.
Example:
#include<stdio.h>
#include<string.h>
int main()
{
char a[50], b[50];
strcpy(a, "Engineering");
strcpy(b, a);
printf("%s\n", b);
return 0;
}
Output: Engineering
Strcat()
The Syntax: strcat(first_string, second_string) function
concatenates two strings and result is returned to first_string.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "This is ", str2[] = “MRCET";
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output:
This is MRCET
MRCET
Strcmp()
The strcmp() function takes two strings and returns an integer.
The strcmp() compares two strings character by character.
If the first character of two strings is equal, the next character of two
strings are compared. This continues until the corresponding
characters of two strings are different or a null character '\0' is
reached.
#include<stdio.h>
#include <string.h>
int main()
{
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
38
double *dp //pointer to double
Pointer Variable Declaration
• The syntax for declaration of a pointer variable is
Datatype *pointer_name;
Example : int *ptr;
• It declares the variable ‘ptr’ is a pointer variable that can point to
an int datatype.
float *ptr;
• It declares the variable ‘ptr’ is a pointer variable that can point to
an float datatype.
int a, *ptr;
ptr = &a;
• The pointer variable should always point to the corresponding
type of data.
Pointer Variable Declaration
Example : float a;
int *p; This is illegal because of the type mismatch
p =&a;
• Assigning absolute address to a pointer variable is prohibited
int *p;
p=5000; //illegal
#include <stdio.h>
int main() a ptr
{ 50 5025
int *ptr, a;
a = 50; 5025 5045
ptr = &a;
printf("Value of a =%d\t", a);
printf("Address of a =%u\n", &a);
printf("Address of ptr =%u\t", &ptr);
printf("Value of ptr =%d", *ptr);
printf(“Address in ptr =%u", ptr);
return 0;
}
OUTPUT:
Value of a = 50 Address of a = 5025
Address of ptr = 5045 Value of ptr = 50 Address in ptr = 5025
Pointer Basic Example – Accessing variable through pointer
#include<stdio.h>
int main()
{
int i=5,*p; i p
p=&i; 5 2002
printf(“Value of i=%d",i);
printf(“Address of i=%u ",&i); 2002 2010
printf(“Value at address of i=%d ",*(&i));
printf(“Address in p=%u ",p);
printf(“Address of p =%u ",&p);
printf(“Value at p=%d ",*p);
OUTPUT
return 0; Value of i=5
} Address of i=2002
Value at address of i=5
Address in p = 2002
Address of p = 2010
Value at p=5
Program on Reference(&) and De-reference operator(*)
#include <stdio.h>
int main()
{
int *pc, c=22;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address in pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n",*pc);
c=11;
printf("Address in pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n",c);
return 0;
}
Output
• Address of c: 26867
• Value of c: 22
• Address in pointer pc:26867
• Content of pointer pc: 22
• Address in pointer pc:26867
• Content of pointer pc: 11
• Address of c: 26867
• Value of c: 2
46
Pointer to Pointer
If a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer
Example :
int a, *p1, **p2;
a=65;
p1=&a;
p2=&p1;
47
Pointer to Pointer Example
#include<stdio.h>
int main()
{
int a, *p1, **p2;
a=65;
p1=&a;
p2=&p1;
printf("a = %d\n", a);
printf("address of a = %u\n", &a);
printf("p1 = %d\n", p1);
printf("address p1 = %d\n", &p1);
printf("*p1 = %d\n", *p1);
printf("p2 = %d\n", p2);
printf("*p2 = %d\n", *p2);
printf("**p2 = %d\n", **p2);
return 0;
}
48
void pointers
• Address of any variable of any data type (char, int, float etc.) can be
assigned to a void pointer variable.
49
void pointer - Example
#include<stdio.h>
int main()
{
int a = 7;
float b = 7.6;
void *p;
p = &a;
printf("Integer variable is = %d", *( (int*) p) );
p = &b;
printf("\nFloat variable is = %f", *( (float*) p) );
return 0;
}
OUTPUT
Integer variable is = 7
Float variable is = 7.600000
50
Pointer Arithmetic
51
Arrays and pointers
• When an array is declared, compiler allocates sufficient amount of
memory to contain all the elements of the array.
• Base address i.e address of the first element of the array is also
allocated by the compiler.
Base address
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2]; // ptr is assigned the address of the third element
printf("*ptr = %d \n", *ptr);
printf("*(ptr+1) = %d \n", *(ptr+1));
printf("*(ptr-1) = %d", *(ptr-1));
return 0;
}
OUTPUT
*ptr= 3
*(ptr+1) = 4
*(ptr-1) = 2
Array of Pointers
char *name[3] = {
"Adam",
"chris",
"Deniel"
};
//Now lets see same array without using pointer
char name[3][20] = {
"Adam",
"chris",
"Deniel"
};