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 Array representation in memory using C Programming language
Previous Home Next

Mainly arrays are categorized in

  • One dimensional array
  • Multidimensional array

# One dimensional Array representation in memory

One dimensional arrays are the simple arrays that we have so far discussed. In previous examples we have shown how to initialize and declare one dimensional array, now we will discuss how it is represented in the memory. As we know now 1-d array are linear array in which elements are stored in the successive memory locations. The element at the very first position in the array is called its base address.

int arr[5];
Now consider the following example :
Element 34 78 98 45 56
Index arr[0] = 100 arr[1] = ? arr[2] = ? arr[3] = ? arr[4] = ?

Above we have defined an array of five elements of integer type whose first index element is at base address 100. i.e, the element arr[0] is stored at base address 100. Now for calculating the starting address of the next element i.e. of a[1], we can use the following formula :

Base Address (B)+ No. of bytes occupied by element (C) * index of the element (i)

/* Here C is constant integer and vary according to the data type of the array, for e.g. for integer the value of C will be 4 bytes, since an integer occupies 4 bytes of memory. */

Now, we can calculate the starting address of second element of the array as :

arr[1] = 100 + 4 * 1 = 104;
/*Thus starting address of second element of array is 104 */

Similarly other addresses can be calculated in the same manner as :

arr[2] = 100 + 4 * 2 = 108
arr[3] = 100 + 4 * 3 = 112
arr[4] = 100 + 4 * 4 = 116
Example
#include<stdio.h>
#include<conio.h>
void main()
{
	int a[] = {20, 30, 50, 70, 100};
	int n, sum=0;
	for ( n=0 ; n<5 ; n++ )
	{
		sum += a[n];
	}
	printf ("%d", sum);
	return 0;
}
Output: 270
Previous Home Next