10 - C Basics - Arrays (Part 1)
10 - C Basics - Arrays (Part 1)
10 - Arrays
/AhmedHatemS
/c/AhmedHatemS
What is the content?
➢Motivational example
➢Arrays
➢Multi-dimensional arrays
Write Write a C program to calculate the average of 3 numbers.
The number of
values in this array
are three values.
Declare that the
values in this The name of the
array are going to array is a. To read
be integers. This any value in this
means that every array, you are
value is going to going to use this
be an integer. name.
Arrays
Array declarationandinitialization:
You can set all values of the array in just one line.
The number of
values in this array
are three values.
Declare that the
values in this The name of the The values in this array.
array is going to array is a. To read You will notice that they
be integers. This any value in this are 3, and they are all
means that every array, you are integers. The first value
value is going to going to use this is 4, the second value is
be integer. name. 7, the third value is 2.
Ways to
initialize array
elements
0 1 2
65 87 30
#define PI 3.14
A Pre-processor directive (#define identifier replacement) ... When the pre-
processor encounters this directive, it replaces any occurrence of identifier
in the rest of the code by replacement.
What about
array’s size?
Passing arrays to functions
Passing arrays:
o To pass an array argument to a function, specify the name
of the array without any brackets:
o int myArray[ 24 ];
o myFunction( myArray, 24 );
o Array size usually passed to function
o Function prototype:
o void modifyArray( int b[], int arraySize );
o Parameter names are optional in prototypes:
o int b[] could be written int [].
o int arraySize could be simply int.
Passing arrays to functions
#include <stdio.h>