0% found this document useful (0 votes)
36 views

Array Presentation in C

The document discusses arrays in C programming. It defines arrays as data structures that store multiple values of the same type sequentially using a single name. Arrays are declared with syntax like datatype name[size], where size is the number of elements. Individual elements are accessed via indexes starting from 0. The document covers how to declare, initialize, access, and pass arrays to functions in C.

Uploaded by

akssoftware
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Array Presentation in C

The document discusses arrays in C programming. It defines arrays as data structures that store multiple values of the same type sequentially using a single name. Arrays are declared with syntax like datatype name[size], where size is the number of elements. Individual elements are accessed via indexes starting from 0. The document covers how to declare, initialize, access, and pass arrays to functions in C.

Uploaded by

akssoftware
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Presentation on

Arrays In C

By Amit Kumar Srivastav


What Are Arrays?
 It is a data structure
 Unlike a variable used to store a single value,
 e.g int debt = 2; debts
debt

1
2
 It is used to store series of values of the same type, sequentially
 The whole array bears a single name, and the individual items, or
elements, are accessed by using an integer index
 e.g
 int debts[10];//int is the datatype
//debts is name of array
//and 20 is the number of elements //contained in an
array
How the Compiler recognizes an Array?

 Just like variables and functions array have to be declared before


their usage.
 Declaration of array;
/* some array declarations */
int main(void)
{
float a[365]; /* array of 365 floats */
char b[12]; /* array of 12 chars */
int c[50]; /* array of 50 ints */
int debts[10]; /* array of 10 ints */
... }
 Above “[]” brackets identifies “a” as an array
 We can access individual elements by writing its index number or
subscript in square brackets along with the array name. cont.
How to Access the Elements of Array

 In C the index of an Array Array Index Array Data


starts at index 0 and continue
until one less then the total i.e. debts[0] 1
(10-1) in case of debts[10]
debts[1] 2
 To access data, just write 1
debts[2]
index of the element, we want,
with the name of the array, e.g . .

 X = debts[2]; . .

. .
/*this will transfer the
value at index 2 of 5
debts array, i.e. 1 to debts[8]
the variable X*/ 8
debts[9]
Data Types of Arrays

 Arrays can be of all the datatype that C allows, some


examples;
int cars[22]; /* an array to hold 22 integers */

char actors[26]; /* an array to hold 26 characters*/

long big[500]; /*an array to hold 500 long integers*/


• Which means the spaces occupied in
memory would be the size of their
respective datatypes for each element of
array, e.g.

int boo[4]; /* an array to hold


4 integers */

char foo[4]; /* an array to hold


4 char */
Initialization of An Array

 A single variable can be initialized as;


int debt = 2;

 Array Initialization;
#define NUM = 8; /*constant, a good prog. practice*/

int main(void)
{
int powers[NUM] = {1,2,4,6,8,16,32,64}; /* ANSI only */

... }

 comma-separated list of values enclosed in braces form


the elements of an array
Initialization cont.
 What if we don’t initialize an array;

/* no_data.c -- uninitialized array */


#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]; /* uninitialized array */
int i;
printf("%2s%14s\n", "i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, no_data[i]);
return 0;
}
 Like ordinary variables if we don’t initialize the array elements they can have any value
already present at that memory location.
The output is;
i no_data[i]
0 16
1 4204937
2 4219854
3 2147348480

Which are values present already in those memory location


Initialization cont.
/* some_data.c -- partially initialized array */
#include <stdio.h>
#define SIZE 4
int main(void)
{
int some_data[SIZE] = {1492, 1066};
int i;
printf("%2s%14s\n", "i", "some_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, some_data[i]);
return 0;
}

 This time the output looks like this:


i some_data[i]
0 1492
1 1066
2 0
3 0
Which means partial initialization, also set the other memory locations to 0;
Initialization cont

 Another way;
const int days[] = {31,28,31,30,31,30,31,31,30,31};
//compiler sets the number of items to 10 automatically

 Designated Initialization;

int arr[6] = {0,0,0,0,0,212}; // traditional syntax


int arr[6] = {[5] = 212}; // initialize arr[5] to 212

if we want to initialize just one element of array,


former is the traditional way, and later is the new
C99 method.
Assigning Values to Arrays

 We can use loops for assigning values to array


in addition to assignment at initialization

 Some wrong methods of assignment;


/* nonvalid array assignment */
#define SIZE 5
int main(void)
{
int o[SIZE] = {5,3,2,8}; /* ok here */
int y[SIZE];
y = o; /* not allowed */

y[SIZE] = o[SIZE]; /* invalid */

y[SIZE] = {5,3,2,8}; /* doesn't work */


}
Array Bounds
 C compiler wont check the boundary of your array, so it’s the
programmer responsibility.
 Reason! To make C faster.
 How to avoid!
 Use a symbolic constant for length of Array index,
 Remember that indexing starts at 0 in C.
int n = 5;
int m = 8;
float a1[5]; // yes
float a2[5*2 + 1]; // yes
float a3[sizeof(int) + 1]; // yes
float a4[-4]; // no, size must be > 0
float a5[0]; // no, size must be > 0
float a6[2.5]; // no, size must be an integer
float a7[(int)2.5]; // yes, typecast float to int constant
float a8[n]; // not allowed before C99
float a9[m]; // not allowed before C99 VLA
Multidimensional Arrays

 Declaration;
int box[2][2];
int box[2][2][2]; //uptil 26 in Bloodshed DevC

 Initialization;
Int box[2][2] = { {1, 2}, {3, 4} }; //two col and rows
Int box[2][2][2] = {
{ {1,2}, {3, 4} },
{ {5,6}, {7, 8} }
};
Passing Arrays to Function

#include <stdio.h>
int sum(int arr[]);
int main(void)
{
int i ;
int arr[4];
long answer;

for (i = 0; i < 4; i++)


arr[i] = 2*i;
answer = sum(arr);
for (i = 0; i < 4; i++)
printf("index = %d, value = %d\n", i, arr[i]);
printf("result = %d\n", answer);
}
int sum(int arr[])
{ int i;
int result = 0;
for (i = 0; i < 4; i++)
result = arr[i] + result;
return (result);}

You might also like