Array ( 1-Dimensional Array)

·

3 min read

  • 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

  1. One Dimensional array.

  2. 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 )

  1. int a[3]; Valid

  2. int a[]; Invalid

  3. int a[4]={1,2,3,4}; Valid

  4. int a[]={1,2,3,4}; Valid

  5. int a[0]={1,2,3,4}; Valid

  6. int a[-3]; Invalid

  7. int a[3.4]; Invalid

  8. int n=4, a[n]; Invalid

  9. int a[4]={1,2,3}; Valid

  10. int a[4]={1,2,3,4,5}; Invalid

  11. int a[0]; ERROR

  12. #define n 5

    int a[n]; Valid

    Note : 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.

  1. const int n=5, a[n]; Invalid

  2. int a[5>3]; 5>3 = true =1

    int a[1]; Valid.

  3. int a[3>5]; 3>5 = false =0

    int a[0]; Invalid

  4. int a[3+2];

    int a[5]; Valid.

  5. int a[5%3]; 5%3 = 1

    int a[1]; Valid

  6. int a[5%5]; 5%5 = 0

    int a[0]; Invalid

  7. int a[1,2,3]; ERROR

  8. int a[40000]; Invalid 40,000 × 2 (size_of_int) = 80,000.

    Note : Stack size is 65536 bytes (64 kb) only.

  9. float a[10000]; Valid 10,000 × 4 (size_of_float) = 40,000.

  10. float a[20000]; Invalid 20,000 × 4 (size_of_float) = 80,000.