Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Presented By : Anil Wadhave CCP Technologies

1) 2) 3) 4) 5) 6) 7) 8) 9)

Definition Why We need of Array ? How to store Define Array ? How to initialize Array? How to store Data in Array? Accessing Elements in a array. Array of Structure Array of Pointer Pointer to array

Definition :
Array is a collection of homogenous data stored in single variable with consecutive memory location.

Why we need array ?


similar data type and we want to write a code for searching and sorting of that data then we need to store that data into contiguous memory so our task would become easy.

When we have to large amount of data of

Eg : student percentage

How to store Array ? Syntax :


Array_type array_name = [list_of _array_elements]; Eg: int a[] ={ 4, 6, 7};

How to initialize Array ?


An array can be initialized in may ways which is shown in bellow.

a) Initialization of array at the time of declaration


int a[] = {2,4,5,8};
Here we not declare the size of array because it will be automatically calculated from number of element initialize. Here we initialize four element so a[] array take a[4] size automatically.

b) Initializing each element separately.


int a[10]; int i; for( i=0; i<10 ; i++) { a[i] = i; }

Here we insert the element at run time and the loop will be execute up to size of array is greater than i.

C)

Initializing array with string

Array can be initialize a string in two different way. These are as . Method 1:

char a[] ={a, n , i, l, \0};


Method 2: char [] = anil;

How to store Data in Array ? int a[10] ={2,4,5,8};

here we take a array a of size 10 and store four element on it. These element are store in continuous memory location is as follows. Index of first element is always 0. And it is denoted as a[0]. After the last element of array, we insert \0 to end the array string.

Accessing elements in an array :

Now we learn how to access these element.

1. #include<stdio.h> 2. int main() 3. { 4. int a[4]; 5. int i; 6. for(i=0; i<4;i++) 7. scanf(enter the array elements : %d \t&a[i]); 8. printf(array elements are : \n) 9. for(i=0; i<10;i++) 10. { 11. printf(%d \t,a[i]); 12. } 13. }

Output :
Enter the array Elements : 4678 Array Elements are : 4678

Array of Structure :
Arrays of structure mean collection of structures, in other word array storing different type of structure member variables.

Memory Representation of array of structure :

For Example #include<stdio.h> struct accesorries { char name[30]; int stock; float price, discount; } void main() { struct accesorries a[3]; int i; float temp; clrscr(); for(i=0; i<3; i++)

}
{

printf("Enter product name :"); gets(p[i].name); printf("\nEnter Stock :"); scanf("%d",&p[i].stock); printf("\nEnter Price :"); scanf("%f",&temp); p[i].price = temp; printf("\nEnter Discount :"); scanf("%f",&temp); p[i].discount = temp;
for(i=0;i<3;i++)

printf("Name=%s, Stock=%d, Price=$%.2f, Discount=%.2f%.\n", p[i].name, p[i].stock, p[i].price, p[i].discount); } getch(); } } }

Output :

Name=Bike, Stock=50, Price=5000, Discount=10.50 Name=Mobile, Stock=100, Price=7500, Discount=10.75 Name=Laptop, Stock=150, Price=8500, Discount=10.55

Array of Pointer :
of pointer means , a pointer is a variable that contains the memory location of another variable. The values you assign to the pointers are memory addresses of the other variables. nothing but collection of address. Syntax :
Array

Data_type * pointer_variable_name

Memory Representation of Array of Pointer.

Int Arr[]

Int *Arr[]
6510 6512 6514

#include <stdio.h> #include <conio.h> main() { clrscr(); int *array[3]; int x = 10, y = 20, z = 30; int i; array[0] = &x; array[1] = &y; array[2] = &z; for (i=0; i< 3; i++) { printf("The value of %d= %d ,address is %u\t \n", i, *(array[ i]), array[i]); } getch(); return 0;}

Output :
The value of
The value of

0 = 10 ,address is 65518
1 = 20 ,address is 65516

The value of

2 = 30 ,address is 65514

Pointer To Array :
A pointer which holds base address of an array or address of any element of an array is known as pointer to array.
eg. int *a[3] here all the elements that is all the 3 elements are pointed by a single pointer.

For Example:
# include<stdio.h> int main() { int arr[4] ={5,10,15}; int *ptr1 =arr; char *ptr2 =(char *) arr; printf(%d %d,*(ptr1 + 1), *(ptr2 + 4)); return 0 }
Output :

10

You might also like