Array ( 1-Dimensional Array)
It is collection of homogenius [same type] variables.
Array is nothing but collection of contiguous memory allocation, where we can store and manage more than one value of same type under one name.
It is derived data type.
It is an Implicit / Internal pointer.
It is implicit constant pointer.
It is one of the data structure.
Advantage
Generally to store several values of same type , we have to declare several variables. Here we have to remember all those variable names also. When the program is too big, it is very difficult to remember all the variable names.
In this situation only the solution is array.
Array reduce program length.
Array minimize the errors.
In function to carry several values of same type at a time, we are using array.
It allows to arrange our data in a order (asc/dec).
Disadvantage
Array size is constant positive integer value. Due to this we are not able to change the array size at the run time.
Some times in causes memory wastage / shortage.
Types of Array in C-Language
One Dimensional array.
Multi Dimensional array.
One Dimensiona Array
An array with one row and several columns.
An array with single subscripting operator [ ] is called one dimensional array.
It is an implicit single pointer.
Syntax
Data_type variable_name[size_of_array] = { elements };
E.g:
int a[4] = {1,2,3,4};
Memory Allocation of Array :
Array is implicit pointer because of array variable stores base cell [ 0 cell 1st byte ] address.
Hence array variable value and 0 cell address both are same.
Array Declaration Methods ( 1-d Array )
int a[3];
Validint a[];
Invalidint a[4]={1,2,3,4};
Validint a[]={1,2,3,4};
Validint a[0]={1,2,3,4};
Validint a[-3];
Invalidint a[3.4];
Invalidint n=4, a[n];
Invalidint a[4]={1,2,3};
Validint a[4]={1,2,3,4,5};
Invalidint a[0];
ERROR#define n 5
int a[n];
ValidNote : A macro in C is like a "shortcut" for a piece of code. It's a way to define something that the C compiler will automatically replace with a specific value or code whenever you use it in your program.
How a macro works:
You define a macro using the
#define
directive.Whenever you use the name of the macro in your code, it gets replaced with whatever code or value you defined for it.
E.g:
#include <stdio.h>
#define PI 3.14 // Define the macro PI to be 3.14
int main()
{
printf("The value of PI is: %f\n", PI); // PI is replaced with 3.14
return 0;
}
Explanation: Here, the macro PI
is replaced by 3.14
wherever it's used. So, when you run the program, it prints 3.14
.
const int n=5, a[n];
Invalidint a[5>3];
5>3 = true =1int a[1];
Valid.int a[3>5];
3>5 = false =0int a[0];
Invalidint a[3+2];
int a[5];
Valid.int a[5%3];
5%3 = 1int a[1];
Validint a[5%5];
5%5 = 0int a[0];
Invalidint a[1,2,3];
ERRORint a[40000];
Invalid 40,000 × 2 (size_of_int) = 80,000.Note : Stack size is 65536 bytes (64 kb) only.
float a[10000];
Valid 10,000 × 4 (size_of_float) = 40,000.float a[20000];
Invalid 20,000 × 4 (size_of_float) = 80,000.