C Array

It is very difficult to define multiple variable names when using more data. So we can create an array to solve this difficulty (single variable name and more data).

  • If the index of the array exceeds its size, the program may crash. Modern compilers usually handle such errors.
  • We cannot initialize the size of an array dynamically. A constant expression is required.

Here we will learn about C Arrays.


Array Rules

  1. Arrays can store only the same data type.
  2. Arrays are always passed by reference.
  3. Array list should end with a semicolon ;. Otherwise, it will show an error.

Base Address of Array

Example:

int a[10] = {0,1,2,3,4,5,6,7,8,9};
  • The starting address of the array is called the base address.
  • If the array starts at address 101, then a[0] is at 101.
  • When we pass an array to a function, the base address is passed.

Array Types

  1. One Dimensional Array
  2. Two Dimensional Array
  3. Multi Dimensional Array

One Dimensional Array

  • When an array has one index, it is called a one-dimensional array.

Syntax

data_type array_name[size];
  • data_type – Type of array elements
  • array_name – Name of the array
  • size – Number of elements

Example Declarations

int a[100];       // int (2Byte), size 100, memory 200 bytes
float s[50];      // float (4Byte), size 50, memory 200 bytes
char name[15];    // char (1Byte), size 15, memory 15 bytes

Reading and Writing

// Reading
for(i = 0; i < 100; i++)
    scanf("%d", &a[i]);

// Writing
for(i = 0; i < 100; i++)
    printf("%d", a[i]);

Array Initialization

data_type array_name[size] = {list_of_values};

Examples

static int age[3] = {10, 20, 30};     // age[0]=10; age[1]=20; age[2]=30
static float salary[] = {1000, 2200, 3300};
static char a[10] = "ArunEworld";     // a[0]='A'; a[1]='r'; ... a[9]='d';

Array Initialization with Zero

  • Partially initialized automatic arrays: remaining elements are set to 0.

Example 1

#include <stdio.h>
int main() {
    int awe[2];
    printf("%d, %d, %d, %d, %d, %d, %d, %d\n",
           awe[0], awe[1], awe[2], awe[3], awe[4], awe[5], awe[6], awe[7]);
    return 0;
}

/* Output: 0, 0, 0, 0, -467742907, 32525, 0, 0 */

Example 2

#include <stdio.h>
int main() {
    int awe[3] = {1, 2, 3};
    printf("%d, %d, %d, %d, %d, %d, %d, %d\n",
           awe[0], awe[1], awe[2], awe[3], awe[4], awe[5], awe[6], awe[7]);
    return 0;
}

/* Output: 1, 2, 3, 0, 0, -2036674747, 32587, 0 */

Accessing Array Elements

#include <stdio.h>
int main() {
    int arr_aew[2] = {10, 2};
    printf("%d\n", 1[arr_aew]);  // Output: 2
    return 0;
}

Two Dimensional Array

  • An array with two indices.
  • We can consider it as a table (rows and columns).

Syntax

data_type array_name[size_1][size_2];
  • size_1 – number of rows
  • size_2 – number of columns

Declaration Example

int a[100][20];   // int (2Byte), 100 rows, 20 columns

Reading and Writing

// Reading
for(i = 0; i < 10; i++)
    for(j = 0; j < 15; j++)
        scanf("%d", &a[i][j]);

// Writing
for(i = 0; i < 10; i++)
    for(j = 0; j < 15; j++)
        printf("%d", a[i][j]);

Array Initialization

data_type array_name[size_1][size_2] = {v1, v2, ..., vn};

Examples

static int mark[3][2] = {60, 70, 80, 35, 90, 18};
// mark[0][0]=60; mark[0][1]=70; mark[1][0]=35; mark[1][1]=90; mark[2][0]=18;

static int mark2[3][2] = {{50}, {0}, {35}};
// mark2[0][0]=50; mark2[0][1]=0; mark2[1][0]=0; mark2[1][1]=0; mark2[2][0]=35; mark2[2][1]=0;

Multi Dimensional Array

  • Arrays with 3 or more dimensions.
  • Maximum dimension depends on compiler.

Syntax

data_type array_name[size_1][size_2]...[size_n];

Example

int a[10][10][10];  // int (2Byte), 10x10x10 elements, memory 2000 bytes

Array Disadvantages

  1. Arrays are static, cannot easily expand or shrink.
  2. Inserting or deleting elements is expensive.

Exercises

1. Find the Number of Elements in an Array

Method 1 (Known Data Type)

#include <stdio.h>
int main() {
    int array[] = {15, 50, 34, 20, 10, 79, 100};
    int n = sizeof(array) / sizeof(int);
    printf("Number of elements: %d\n", n);
    return 0;
}

Method 2 (Unknown Data Type)

#include <stdio.h>
int main() {
    int size, i;
    int Arr[5] = {88, 97, 65, 43, 77};
    size = 5;
   
    printf("\nBefore Array value:\t");
    for(i = 0; i < size; i++)
        printf("%d\t", Arr[i]);
    
    getchar();
    return 0;
}

2. Sum of 10 Elements in an Array

#include <stdio.h>
#include <conio.h>

int main() {
    int num_array[10], i, sum = 0;
    int *ptr;

    printf("\nEnter 10 Elements:\n");
    for(i = 0; i < 10; i++)
        scanf("%d", &num_array[i]);

    ptr = num_array;

    for(i = 0; i < 10; i++) {
        sum = sum + *ptr;
        ptr++;
    }

    printf("\nSum of the array's 10 elements: %d", sum);
    return 0;
}

Previous & Next

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading