array in c programming language

Array in c programming language – Creating, Initializing and types of arrays

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Array in c programming language

Hello Guys, today you are going to learn to Array in c programming language. Here you can learn how to create an array, initialize an array and all kinds of the Array in c programming language.

Suppose you are creating a program that stores employees’ name in the computer. Now suppose your company has 200 employees. How do you store the names of these 200 employees? If you are thinking that you will create 200 variables then this will be a very complex approach.

In this, it will take a lot of time to make the program and the program will be much larger than the extent. This is the wastage of both your time and computer memory space. At the same time, you can not think of the names of so many variables, and if you think of them then they cannot remember to use them in the program.

I have an even better approach for you and that approach is called an array in c programming language. An array is a collection of values of the similar type. So with the same type, I mean the same data type, such as int, float, char etc.

C provides such facility through arrays that you just create a variable and store the name of 200 employees (or whatever information you want to store) in that variable. You might be wondering how to store so many names in a variable. I would be very happy to tell you about it. But before that let’s see how to create an array in c programming language

array in c programming language

Creating C Array in c programming language

An array is a structured data type. As you have read in the tutorial C Structures, you can create many variables in one structure. Here too, something is the same. Whenever you create an array in c programming language, you define the name of the array and how many values it is going to store in it.

As if you want to store 5 numbers, you can create an array in c programming language for that. The general structure of creating arrays in C is given below.

data_type array-name[size];
By size, you define how many values you want to store. An example of this is being given below.
int num[5];
In the above example, the name of the array is num and you can store any 5 integer values.

Initializing C Array in c programming language

So far I have told you how to create an array in c programming language. Now your most important question is that you will store 5 values in this array. So let me tell you that whenever you create an array, then whatever the size of the array, the same array is allocated to the memory. And those locations are allocated by the same number of index numbers.

Index numbers are a unique name for every location. As if in the example above, there are 5 index numbers. One thing you should always remember is that the index of the array in c programming language always starts with zero.

num[0]
num[1]
num[2]
num[3]
num[4]

With Array’s name and index number, you can store a value in every location you have created and later you can also value access to it.

(Note: Index of an array in c programming language always starts with zero.)

Like you can insert value in the array created above.

num[0] = 50;
num[1] = 100;
num[2] = 150;
num[3] = 200;
num[4] = 250;

You can assign all values to each location instead of assigning values to each other at the same time. You can do this in such a way.

int num [5] = {50,100,150,200,250};

And if you want the values to be stored in runtime from the user, then you can do so.

for(int i=0; i<=5; i++)
{
    scanf(“%d”,&num[i]);
}
array in c programming language

Accessing Array Elements

So far, I have told you to create arrays and store values in them. In this section, you will be asked to access those values. By accessing Array elements you can perform all those operations on them that you can perform with normal variables. For example, you can add 2 array elements.

Wherever you want to access the array element, you type the name of the array in c programming language and the index number. For example, see the below-mentioned statement.

num[2] = num[0] + num [1];

The 2 values introduced in the above statement are added by adding to 3 location.

If you want to print one of the values, you can do this as follows.

printf(“%d”,num[3]);

The statement given above will print the value of the 4th location, which is 200 in the example given here.

If you want to print the entire array in c programming language together, then you can do this with the help of a loop. Keep in mind that the loop runs as much as the values are in your array. An example of this is being given below.

for(int i=0; i<=5;i++)
{
    printf(“%d”,num[i]);
}           

Example

#include <stdio.h>
#include <conio.h>
int main()
{
     int arr[5];
int i;
     printf(“Please enter 5 array elements : \n”);
     for(i=0;i<=4;i++)
     {
         scanf(“%d”,arr[i]);
     }
     printf(“Array is:”);
     for(i=0;i<=4;i++)
     {
          printf(“%d\t”,arr[i]);
     }
     return 0;
}

The above program produces the below-given output.

Please enter 5 array elements : 5 4 3 2 1
Array is : 5 4 3 2 1

 

 

Two Dimensional Arrays

A normal array is stored in the form of the data list, which is followed by a second element. If you want to store data in tabular form, you can create a two-dimensional array in c programming language for it. For example, you want to store the id of 4 employees and their phone number. For this, the table will be formed in this way.
101
7599393
102
9384940
103
9458940
104
9129399

This type of table can be created in memory by the two-dimensional array in c programming language.

array in c programming language

Creating Two Dimensional Array

The general syntax for creating a two-dimensional array in c programming language is being given below.
data-type array_name[row][column];

 

The two-dimensional array in c programming language is defined in the context of row and column. First, you define the type of array. After this, you define a unique name of the array. After this, you will define the number of angular brackets as many as you want to create. After this, you will define the number of columns you want to create in the second angular bracket.
For example, for the above table, you will create a memory through the two-dimensional array in c programming language in this way.
int myArray[4][2];
The statement given above will create a table (two-dimensional array) in memory with 4 rows and 2 columns.

Initializing Two Dimensional Array

You can initialize the two-dimensional array in c programming language as a variable. When you initialize this way, assign the value to each position separately. For example, if you want to store the value in the first column of the first row, then you will do this.
myArray[1][1]=101;
Similarly, you can store the phone number in the second column as follows.
myArray[1][2]=7599393;
If you want to input the value from the user to this array in c programming language, you will use 2 loops for this. It will initialize the first loop rows and it will only run to the number of the row. The second loop will iterate the columns and it will also be run to the number of columns. An example of this is being given below.
for(int i=0;i<4;i++)
{
for(int j=o;j<2;j++)
{
scanf(“%d”,myArray[i][j]);
}
}

Accessing Two Dimensional Array

If you want to access a single element from the two-dimensional array in c programming language, you can do this by accessing its position index. For example, if you want to print the value of the 1st column of the 2nd row, then for this, you will write a statement like this.
printf(“Element at 2nd row 1st column is :%d”,myArray[2][1]);
If you want to print all the elements of the array in c programming language at a time, you use 2 loops for it. An example of this is being given below.
for(int i=0;i<4;i++)
{
for(int j=o;j<2;j++)
{
printf(“%d”,myArray[i][j]);
}
}

Example

#include <stdio.h>
int main()
{
     int myArray[4][2];
     int i,j;
     printf(“Please enter array elements : “)
   for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&myArray[i][j]);
}
}  printf(“Arrays is : \n”)

for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,myArray[i][j]);
}
printf(“\n”);
}
}
The above program produces the below given output.
Please enter array elements : 1 2 3 4 5 6 7 8
Arrays are:
1  2
3  4
5  6
7  8

 

Passing Array to Function

If you want to pass an array in c programming language as an argument in any function, it can also pass. For this, the general syntax for declaring the parameter in the function is being given below.
return-type function-name(array-type array-name[])
{
//Function code here…
}

Passing an array in c programming language in the functions is being explained by a simple example.

#include<stdio.h>int sum(int num[], int size);

void main()
{
int num[5] = {2,2,2,2,2};

int res;

res = sum(num,5);

printf(“Sum of arrays is %d\n”,res);
}

int sum(int num[], int size)
{
int i;
int result=0;

for(i=0;i<size;++i)
{
result += num[i];
}

return result;
}

The above example produces the below given output.

Sum of the array is 10

must read:- http://www.visitorsdetails.com

Returning Array from Function

The array can also be returned from any function other than the normal variables. Returning to a whole array in c programming language is not allowed but you can return the pointer of the array. For this, you have to specify the name of an array with no index.

One important thing you should keep in mind is that the return of the address of a local variable of the C language function does not allow it to be returned. Therefore, whatever you want to return, you have to define static.

The function of which you want to return the array in c programming language, you will have to declare it with the pointer. Because, as I mentioned earlier, the pointer return of an array is allowed only in the C language. Its syntax is being given below.

return-type * function-name
{
//Function code here…
}

An example of returning an array from function to C language is being given below.

#include<stdio.h>int* table(int num)
{
static int tbl[10];
int i;for(i=1;i<=10;i++)
{
tbl[i] = num*i;
}return tbl;}int main()
{
int *t;
int num;
int j;printf(“Please enter a number to generate its table.\n”);
scanf(“%d”,&num);t=table(num);printf(“Table of %d is given below : \n”,num);for(j=1;j<=10;j++)
{
printf(“%d\n”,t[j]);
}return 0;
}

The above example produces the below given output.

Please enter a number to generate its table.
2
Table of 2 is given below :
2
4
6
8
10
12
14
16
18
20

Pointer to Array

An array has a name constant pointer. It points to the first element of the pointer array in c programming language. Whenever you create an array, it becomes automatically created the pointer.

So if you assign an array name to a pointer variable, then you do not need to define the address of (&) operator before the name of the array in c programming language. Because as I mentioned earlier an array name is already a constant pointer.

For example, you have created an array named Num, which has 5 integer numbers.
int num [5] = {1,2,3,4,5};

If the name of the same array in c programming language is assigned to an integer pointer variable, then it will point to the first element 1 of the variable Num array.

int *n;n=Num;

In this situation, the pointer variable addresses the address of the first variable of the array in c programming language. If it is used with value at (*) operator, it prints variable 1. Similarly, you can access all the elements of an array by this pointer variable.

A simple example of Array and Pointers is being given below.

#include<stdio.h>int main()
{
int Num[5] = {1,2,3,4,5};
int *n;n=Num;printf(“%d”,*n+2);return 0;
}

The above example produces the below given output.

3

 


Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

9 comments

  1. Hi there! Dо you use Twіtter? I’d like t᧐ follow you if that wouⅼd be
    okay. I’m absolutely enjoying your blog and look forward to new updates.

Leave a Reply

Your email address will not be published. Required fields are marked *