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

ArrayPresentation in C

The document is a presentation on arrays in C that covers: - What arrays are and their types including 1D, 2D, and 3D arrays - How arrays are declared and recognized in C code - Data types that can be used for arrays - How arrays are allocated in memory - Different ways to initialize and assign values to array elements - Array bounds and avoiding out-of-bounds errors - Passing arrays to functions - Representation of multidimensional arrays

Uploaded by

Arush Swaroop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

ArrayPresentation in C

The document is a presentation on arrays in C that covers: - What arrays are and their types including 1D, 2D, and 3D arrays - How arrays are declared and recognized in C code - Data types that can be used for arrays - How arrays are allocated in memory - Different ways to initialize and assign values to array elements - Array bounds and avoiding out-of-bounds errors - Passing arrays to functions - Representation of multidimensional arrays

Uploaded by

Arush Swaroop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Presentation on

Arrays In C

By:-
Arush Swaroop
100101051
CS-C
Index
What are Arrays?
Types of Arrays
1-D Arrays
Recognising Arrays
Data Types of Arrays
Space Allocation
Assigning Values to Arrays
Multidimensional Arrays
What Are Arrays?
 An Array is a systematic arrangement of objects, usually in rows and
columns.

 It is a data structure.

 Unlike a variable used to store a single value.


Example int debt = 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
that are accessed by using an integer index .
Type of Arrays
1-Dimensional Array
2-Dimensional Array
3-Dimanesional Array
1-Dimensional Arrray
Example
int debts[10];
“int” is the Datatype.
“debts” is name of Array.
“10” is the Number of Elements in an Array.

Lower bound: the smallest element of an array's index.

Upper bound: the highest element of an array's index.

Range: the number of element in the array.


Range = upper bound – lower bound+1
How the Compiler Recognizes an Array?
 Just like Variables and Functions, Arrays 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 characters)
int c[50]; (array of 50 integers)
}
 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.
How to Access the Elements of an Array

 In C the index of an Array starts at Array Index Array Data


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

 Example . .

. .
X = debts[2];
debts[8] 5
(this will transfer the
value at index 2 of 8
debts array, i.e. 1 to debts[9]
the variable X)
Data Types of Arrays
 Arrays can be of all datatypes that C allow.

 Examples:-

int cars[22]; (an array to hold 22 integers)

char actors[26]; (an array to hold 26 characters)

float big[500]; (an array to hold 500 decimals)


Space Allotment of an Array
 The spaces occupied in memory would be the size of their respective datatypes for
each element of array.
 Example:-

int boo[4];
(an array to hold 4 ints)

char foo[4];
(an array to hold 4 chars)
Initialization of An Array
A single variable can be initialized as;
int debt = 2;
Array Initialization
#define NUM = 8;

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

... }
 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


 Partially Initialized Array
/* 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 partially initialization, also set the other memory locations to 0;
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 assigning;


/* 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];

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);}
Representation of a Multidimensional
Array
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy