Professional Documents
Culture Documents
Array in C
Array in C
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.
Eg : student percentage
Here we insert the element at run time and the loop will be execute up to size of array is greater than i.
C)
Array can be initialize a string in two different way. These are as . Method 1:
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.
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.
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++)
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
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