C Programming language

C Standard Library Function

Day 1: Introduction and brief history of C Programming language

Day 1: Advantages and Disadvantages of C, C Keywords, Data type modifiers in C

Day 1: Data types in C Programming language

Day 1: Secondary data types, Primitive and Non-primitive data types

Day 1: C Variables, C Constant, Format Specifiers in C

Day 2: Write first C program

Day 2: Flow of C program with example, main(), printf(), scanf()

Day 2: Operaters in C Programming language, Arithmetic operators

Day 2: Relational operators and Logical Operators in C Programming language

Day 2: Assignment, Increments and Decrement Operators in C Programming language

Day 3: Conditional statement: if else statement in C Programming language

Day 3: Conditional statement: switch statement in C Programming language

Day 3: Jump statements: return statement in C Programming language

Day 3: Jump statements: go to statement in C Programming language

Day 3: Jump statements: break statement in C Programming language

Day 3: Jump statements: continue statement in C Programming language

Day 4: Loops OR Iteration statement in C Programming language: for Loop

Day 4: Loops OR Iteration statement in C Programming language: while Loop

Day 4: Loops OR Iteration statement in C Programming language: do while Loop

Day 5: Array in C Programming language

Day 5: Access elements of Array in C Programming language

Day 5: One dimensional Array representation in memory using C Programming language

Day 5: Two dimensional Array representation in memory using C Programming language

Day 5: Multidimensional Array in C Programming language

Day 6: Function in C Programming language

Day 6: Definition, Declaration and Calling a Function in C Programming language

Day 6: Passing array to a function in C Programming language

Day 6: Calling Function in C Programming language : Call by value

Day 6: Calling Function in C Programming language : Call by reference

Day 6: Recursive Function in C Programming language

Day 6: Adding function to the library in C Programming language

Day 7: Pointer in C Programming language, How to use Pointer, Pointer declaration

Day 7: NULL Pointers in C Programming language

Day 7: Array of Pointers in C Programming language

Day 7: Pointer arithmetic in C Programming language

Day 7: Pointer to Pointer in C Programming language

Day 7: Pointer to Function in C Programming language: Passing pointers to functions

Day 7: Pointer to Function in C Programming language: Return pointer from functions

Day 8: Strings in C Programming language, Declaring String in C Programming language

Day 8: String functions in C Programming language

Introduction of Structure

Accessing the members of Structure

Structure With typedef Keyword and Use of sizeof function

Example of Structure

Dynamic memory allocation in C: Introduction

adplus-dvertising
Day 5 What is an Array ?
Previous Home Next

Array

An Array may defined as the collection of the homogeneous (same type) elements that are stored in the contiguous memory locations. For example, if we have to store 7 values of any type, lets say, integer values we don't have to declare 5 variables with different identifiers but we can define an array of integer type and can store the values in array directly with a unique identifier. Thus, an array named arr created in memory having 5 values can be represented as :

Element12345
Index01234

Cells have been assigned values from 0 to 4 as in arrays values are stored from a base index of 0.

How to declare an Array ?

An Array can be declared with the help of an unique identifier specifying its data type and also by specifying the number of elements it must contain. The number of elements are declared inside a square bracket i.e. []. Thus when an array of integers having 5 elements considering the above figure can be declared as :

int arr[5];   
/*the element field within the [] should be a constant value
as the are the blocks of static memory whose size must be declared
before execution. For variable length array dynamic memory is 
required which we will discuss later.*/

How to initialize an Array ?

While declaring the array for a local scope e.g. for a particular function its contents will not be initialized by default and will remain undetermined till we initialized it separately. In case of array of global scope the values are initialized but having the default values of 0's for each element.

Thus, for the array of both scopes we can initialize an array by enclosing the elements within the {} brackets. For example,

int arr[5] = {1,2,3,4,5}; 
/* This array will be created as which one is shown in the above figure. */
char string1[]="first";

char string[]={'f','g','t','y','u','I','\0'};
// Here ,we using char string and its array size is six.
// '\0' null character terminating the string.

The no of elements declared within the {} must not exceeds the numbers of elements defined in the [], else it could some serious trouble to your program.

What are the advantages of using an Array ?

Following are main advantages of using an array :

  1. Arrays are the most convenient way of storing the fixed amount of data that can be accessed in an unpredictable fashion
  2. Arrays are also one of the most compact data structures as in for storing the 50 elements in an array of integer type it will take the memory equivalent to the amount of memory required to store 50 elements in addition to that a few overhead bytes for whole array.
  3. Another advantage of array is that iterating through an array is more faster than compared to other data structure types like linked list because of its good locality of reference.
Previous Home Next