How To Set An Array In C

Table of contents:

How To Set An Array In C
How To Set An Array In C

Video: How To Set An Array In C

Video: How To Set An Array In C
Video: C Programming Tutorial 81 - Intro to Arrays 2024, November
Anonim

Arrays in the C and C ++ programming languages are most often used to form a sequence of data of the same type. This organization of the parameters most effectively allows you to solve the assigned tasks. Especially in the C and C ++ programming languages, where arrays can be specified both at the beginning of a program and anywhere in its code. The main thing is to take into account the scope of the created variables.

How to set an array in C
How to set an array in C

Instructions

Step 1

An array, as a named set of data of one type, occupies a well-defined place in memory, with each subsequent element located immediately after the previous one. A specific cell is accessed by index; in C, the first element has index zero. When describing, one should take into account the dimension of the array, i.e. one-dimensional or two-dimensional, containing two strings, the array will be used.

Step 2

Determine the scope of the generated array. If it will belong to one local function, write its name and size at the very beginning when declaring other variables. When forming a global array, its description should be performed at the very beginning of the program or in the included header file (h-file).

Step 3

In C, an array is defined by a unique name that indicates the type of data stored in it, as well as the dimension in the single or double operator . Create a one-dimensional array that has one row.

An example of creating a one-dimensional array:

double m_P1 [200];

char m_C1 [20];

In this case, two one-line arrays m_P1 and m_C1 have been created. The first one stores 200 variables of the double type, and the second one - 50 character values (char).

Step 4

Specify a two-dimensional array (matrix) where two indices must be specified in the operators to dereference a specific element. The syntax for describing such an array is similar to one-dimensional, except for specifying the dimension.

An example of creating a two-dimensional array:

double m_P2 [100] [50];

char m_C2 [20] [10];

Step 5

However, for multidimensional arrays in the C language, there are concessions in terms of specifying the exact parameters of the dimension. If the two-dimensional array is initialized simultaneously with the declaration, it is permissible not to specify the first dimension, i.e. the number of lines in the array.

int m_I [4] = {{3, 7, 9, 2}, {4, 1, 2, 1}, {3, 8, 9, 4}, {5, 1, 3, 9}};

In this case, the exact size of the m_I array will be determined by the compiler directly when linking the executable program.

Recommended: