Lesson 11
( Arrays Part 1)
Hi and welcome on learn for free programming!
In this lesson we are going to talk about arrays. So far we have placed emphasis on the scalar data types such as integers, characters, and real numbers. This lesson is gong to be in two parts because there are many thing's to tell about arrays. So we are going step by step so it would be much easier for you to follow and understand.
Basics for arrays
Array is a continuous arrangement of objects of the same type. The type of array of the stems from the type of its elements. In C language, beginning of array is element with index 0. If you have programmed in Pascal or some other languages that the index of the first element of taking one, it might take a little while to get used to this approach.
![]() |
| Picture 1. Basic array |
Declaration of arrays
As all other variables in the program, you have to declare array before you use it. Declaration of array tells the compiler for C what needs to know about the array.
That is :
- Type
- Name
- Dimension
Declaring an array looks like this:
type name_of_array [ dimension ] ;
Component name of array is the name which allows you access to array. Component dimension of the array must be a constant value or a constant expression. Type is the type of array elements ( int, char,.. ).
Initialization of array
Initialization of array means that every element of array get's value 0. Initialization of array can be done in two ways.
- example 1:
#include <stdio.h>
#include <conio.h>
int main()
{
int x[10]; //-------> defining the array
int i; //-----> we are going to use i as index ( to go trough the array )
for(i=0;i<10;i++) x[i]=0; //-------> assigning value 0 to all elements of array
getch();
}
In example 1 you see in for loop x[i]. That means the value in x array on index i.
![]() |
| Picture 2. Array |
If you write printf("%d", array1D[1] ); it will display on the screen the value 10 ( look picture 2 ).
- example 2:
#include <stdio.h>
#include <conio.h>
int main()
{
int x[10]={1,2,3,4,5,6,7,8,9,10}; //-------> defining the array and initializing (assigning value 0 to elements)
getch();
}
Okay that's it for this lesson come back to learn more about C.
I will be posting more lessons about C programming, so follow me, comment if there is something you would change :) .
If something doesn't work or you need help just email us : sashans89@gmail.com or sasha97ns@gmail.com and we will help you.
See you next time, Bye.
See you next time, Bye.
Zoran


No comments:
Post a Comment