Array

EmbLogic RCD Labs
2 min readMar 27, 2020

--

Array is a collection of similar types of data item in contiguous memory locations.Array can store the primitive type of data such as integer,character,double,float.c array is beneficial when we have to store similar types of elements.Array store

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows −

Type array name [size];

This is called a single-dimensional array. The array Size must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double, use this statement −

double balance[10].

Here balance is a variable array which is sufficient to hold up to 10 double numbers.

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.

Double salary = balance[9];

The above statement will take the 10th element from the array and assign the value to salary variable.

Two-dimensional Arrays

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows −

type arr[x][y];

Where type can be any valid C data type and arr will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns.

where arr is the name of an array.

Accessing Two-Dimensional Array Elements

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example

int val = arr[2] [3]

The above statement will take the 4th element from the 3rd row of the array.

Passing arrays as function argument in C

If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. Similarly, you can pass multi-dimensional arrays as formal parameters.

Passing argument ways

void func(int *ptr);

Return arrays from function in C

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array’s name without an index.

Return *FunctionName();

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

EmbLogic RCD Labs
EmbLogic RCD Labs

Written by EmbLogic RCD Labs

0 Followers

EmbLogicTM is a technology design house and professional training company, providing research, competency development and customised training solutions.

No responses yet

Write a response