Fields in C

from Wikipedia, the free encyclopedia
Visualization of an integer field

The article Fields in C describes the use of fields in the C programming language . In contrast to variables and constants , a C programmer can store not just one but several values ​​of the same data type in fields .

definition

variants

A field can be defined in different ways. The first variant consists of three parts. First from the field type, which must match the data type of the values ​​it contains. Secondly, from the desired name of the field and thirdly from the number of saved values ​​in square brackets. The general scheme of the first variant is FIELD TYPE FIELDNAME [INDEX] .

int ganzzahlen[3];               /* Definition eines Feldes, das drei Werte als Integer enthält */

In the above variant, the individual field values ​​are not initialized by the programmer. The compiler also only initializes the field elements with 0 if they are global or static fields; in all other cases the elements are not automatically initialized with 0. As an alternative to the first variant, the field values ​​can be initialized when defining the field. In this case, the compiler automatically determines the index as the number of values ​​passed.

int ganzzahlen[] = { 1, 2, 3 };  /* Definition eines Feldes, das die Werte 1, 2 und 3 als Integer enthält */

In another variant, the index can be specified in addition to the values ​​specified in the definition. In this case, all field elements not specified are initialized with 0.

int ganzzahlen[5] = { 1, 2, 3 }; /* Definition eines Feldes, das die Werte 1, 2, 3, 0 und 0 als Integer enthält */

A final variant of defining a field is only possible for fields of the field type char . It is used to initialize fields with a character string . The compiler automatically adds the value 0 to the end of the specified string.

char zeichen[] = "test"; /* Definition eines Feldes, das die Werte t, e, s, t und 0 als Character enthält */
                         /* gleichbedeutend, aber umständlicher sind folgende Ausdrücke:                  */
                         /*       char zeichen[5] = { 't', 'e', 's', 't' };                               */
                         /*       char zeichen[] = { 't', 'e', 's', 't', '\0' };                          */

Components

All predefined data types except void and self-defined data types can be used as the field type. The same rules apply to the selection of a field name as to the selection of a variable and constant name. The values ​​contained in the field are also referred to as objects or elements of the field.

The number of values ​​stored in the field is called the index of the field. The index of a field can no longer be changed after definition, so a field is the same size during the entire runtime of the program. An index always starts at zero. This means that the first field element is at index 0, the second at index 1, and so on. Since an index always starts with zero, it is not possible to create a field with indices 100 to 200, for example. Since the field length can be required at different points in the program, it is usual to define it only once at the beginning of the program as a preprocessor directive .

#define FELDLAENGE 3
int ganzzahlen[FELDLAENGE];

The field length can be determined using the unary operator sizeof .

int feld[3];
size_t feldgroesse_in_byte = sizeof(feld);
size_t feldelementgroesse_in_byte = sizeof(feld[0]);
size_t feldindex = feldgroesse_in_byte / feldelementgroesse_in_byte;

Storage space

When defining a field, the compiler automatically requests the required memory space. The memory address at which the field is located cannot be changed by the programmer. The size of the memory required depends on the data type of the field elements. For example, a field that contains three integers uses three times the storage space that an integer requires.

The field elements are located one behind the other in the memory. The address of the field is exactly the same as that of the first field element. The address of the second array element is the address of the first element plus one, and so on.

Value assignment

After a field has been defined, values ​​of the data type specified in the definition can be assigned to it. The index at which the value is to be located must also be specified.

ganzzahlen[0] = 23;            /* Zuweisung des Wertes 23 an der Indexstelle 0 */
ganzzahlen[1] = 1;
ganzzahlen[2] = 1;
ganzzahlen[0] = 22;

Often, field values ​​are assigned using the for loop .

int ganzzahlen[3];
for (int i = 0; i < 3; i++) {  /* Nach der Schleife steht am Index 0 der Wert 1, am Index 1 der */
    ganzzahlen[i] = i + 1;     /*  Wert 2 und am Index 2 der Wert 3 */
}

If the index is read or written outside of the index, the program still tries to access the relevant memory address. If the operating system detects that the program is trying to access a memory address that is not assigned to it, the program may crash. If the memory address is randomly assigned to the program, the data from this memory location could be read out or overwritten without an error message.

int ganzzahlen[3];             /* Da der Index 82 nicht existiert, kann es zu einem  */
ganzzahlen[82] = 23;           /* Programmabsturz kommen bzw. können Daten überschrieben werden */

Constant fields

If the values ​​of a field are to be unchangeable, a constant field is defined.

const int ganzzahlen[3];

Constant fields only make sense with initializations (when defining):

const int ganzzahlen[3] = { 4711, 4200, 0815 };

Multi-dimensional fields

In C it is also possible to use multidimensional fields. Each field element is itself a field.

int mehrdimensional[3][4];       /* Zweidimensionales Feld */
int mehrdimensional[3][4][15][2] /* Vierdimensionales Feld */

As with the one-dimensional field, the field values ​​of a multi-dimensional field are stored directly one after the other in the memory. The values ​​are also accessed in the same way.

int ganzzahlen[2][2];
ganzzahlen[0][0] = 23;
ganzzahlen[0][1] = 15;
ganzzahlen[1][0] = 2;
ganzzahlen[1][1] = 0;

Fields as function parameters

In C, both the value of a variable and a pointer to the variable can be passed to a function. On the other hand, no copies of fields can be transferred, only pointers to their beginning. The function receives a copy of the memory address of the start of the field. Since the function receives a pointer, it can read and overwrite the field elements.

feld[3];
funktion(feld);        /* auch ohne explizite Angabe der Funktion ein Zeiger auf das Feld übergeben */

Usually the index of the field is also passed to the function. This is due to the fact that the field size can no longer be determined within the function, since the field within the function is actually no longer a field, but a constant pointer.

feld[3];
funktion(feld, 3);     /* auch ohne explizite Angabe der Funktion ein Zeiger auf das Feld übergeben */

The function declaration specifies that the function expects, for example, a one-dimensional field of the field type integer as a parameter. If a field index is specified in the function declaration, this specification is ignored by the compiler. The index is therefore transferred as a separate parameter.

void funktion(int feld[], int index);
void funktion(int  *feld, int index); /* gleichbedeutend */

In the case of multi-dimensional fields, the expected indices of all dimensions except the first must be specified in the function declaration.

void funktion(int feld[][10][7], int index);
void funktion(int (*feld)[10][7],int index); /* gleichbedeutend */