C language for MSX – lesson 5

da | Jan 3, 2024 | C language | 0 comments

C language for MSX

Column by Nicola Brogelli (Part 5)

Arrays and Pointers

Arrays

Arrays are sequences of variables of the same type that are stored consecutively in memory and which can be accessed using the same name (identifier) to which an index is added. The main use of arrays is to store and organize a large number of similar elements into a single “data structure”, making them easier to manage and manipulate.

An “object” of type array is a n-tuple of ordered elements of the same type, which can be accessed via their index. The elements of a dimension array n are included in a range from 0 to – 1.

type identifier [constant-expressio]

The definition illustrated above represents the declaration of a one-dimensional array (we will see multidimensional arrays later). We can see how the array has a well-defined type which describes the elements that the array can contain, an identifier which is the name of the array and a numerical value greater than zero which defines its size. An array can be initialized at the same time as its declaration by listing the comma-separated values inside braces. Or it can be declared and subsequently initialized.

In the following example it is not necessary to give a size to the array as it is explicitly initialized (at the same time as the declaration we initialize the array with the values reported in curly brackets).

int numbers[] = {1,2,3,4,5};

Arithmetic and comparison operations are not defined on arrays nor can they be returned by a function, the only defined operation is selection with index (arrays can be used as arguments of a function, we will see some examples in the next handout).

The following instructions state that numbers is an array of integers with size 5, and then the value 7 is assigned to element number four of the array.

int numbers[5];
numbers[3] = 7;

We must be very careful because the language does not carry out any checks on the values of the indexes, the following example would not give any errors at compile time, but could generate problems at run time. In fact, we would write the value 7 in an area of memory whose meaning we do not know.

int numbers[5];
numbers[5] = 7;

Arrays and Pointers

This is a bit more complex topic, but I will try to describe it as simply as possible. The name of an array (in the previous case it was numbers) is also the memory address where the first element of the array itself is located. Therefore we can say that if numbers[n] is an array then &numbers[0] e numbers represent your address in a similar way. In the portion of code below the two printouts will show the same memory address.

int numbers[] = {1,2,3,5,6};
printf(“\nprint numbers: %d”, numbers);
printf(“\n print &numbers[0]: %d”, &numbers[0]);

If an integer value i is added to the array name, the result will be a memory address given by the base address of the array plus an offset given by i. In the following example both prints will return the same memory address (the substantial difference with the previous example lies in the offset i).

int numbers[] = {1,2,3,5,6};
int i = 2; // i must be a number between 0 and 4
printf(“\nprint numbers: %d”, numbers + i);
printf(“\n print &numbers[i]: %d”, &numbers[i]);

While *numbers And numbers[0] similarly represent the first element of the array (value). So both printouts shown in the code portion will give the same result.

int numbers[] = {1,2,3,5,6};
printf(“\n print *numbers: %d”, *numbers); // will print 1
printf(“\nprint numbers[0]: %d”, numbers[]); // will print 1

As for the addresses, also in the code example below, to obtain the value of an i-th element of the array, we added the offset i to numbers. Both prints will return the same result.

int numbers[] = {1,2,3,5,6};
int i = 2; // i must be a number between 0 and 4
printf(“\n print *(numbers + i): %d”, *(numbers + i)); // will print 3
printf(“\n print numbers[i]: %d”, numbers[i]); // will print 3

So in summary, the first element of an array numbers[n] is referred to by the expression *numbers, L'the-th element of the array is referred to with the expression *(numbers + i). But more generally if ind is an address and any reference to ind[i] can be expressed in the form *(adr + i).

Arithmetic and Pointers

Let's try to give further insight into the pointer arithmetic that we mentioned in the previous examples. Using pointer arithmetic we could access the various elements of the array as seen previously. Incrementing a pointer increments the memory address contained in the pointer causing it to point to the next element. To do this, C knows the type of data pointed to and increments the address based on its size. So let's assume that pi be a pointer to an array of integers with the statement pi++ the value of pi is incremented by the size of the int type (generally 2 bytes). Likewise if pf points to an array of floats, pf++ will increment its value by the float size (generally 4 bytes). This also applies to increments greater than unity. For example, if we add a value to a pointer, C increases by the number of bytes needed to reach the new memory address. So:

pi +=4

will increase the content of pi of 8 bytes (always assuming that an int is 2 bytes).
All the reasoning done up to this point applies equally to the decrement which is carried out using the operators — or -= .

The last operation foreseen is the differentiation between two pointers that point to two different elements of the same array, this allows us to know how much they differ from each other.
So if pi1 And pi2 are two pointers to the same array of integers, the expression pi1 – pi2 provides the distance between the two of them.

All relational operators such as ==, !=, >, < etc. are also valid as long as both pointers point to the same array.

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

Linguaggio C per MSX – lezione 6

C language for MSX – lesson 6

C language for MSX Column by Nicola Brogelli (Part 6) Arrays and Pointers Multidimensional Arrays Until now we have considered one-dimensional arrays, which required only one index to identify an element. C allows you to...

Linguaggio C per MSX – lezione 3

C language for MSX – lesson 3

C language for MSX Column by Nicola Brogelli (Part 3) Enumerations Enums or enumerations are particular types of data that contain a list of constants, each of which is associated with an integer value. Specifically, the constant values contained...

MSXgl una libreria di giochi scritta in C

MSXgl a game library written in C

MSXgl is a game library written in C and intended for MSX computers. The goal of this library is to give C programmers the full feature set to create a high-performance game. Even though a C program can never be efficient...

Linguaggio C per MSX – lezione 3

C language for MSX – lesson 2

C language for MSX Column by Nicola Brogelli (Part 2) Type Conversions As we saw in the previous handout, in C every piece of data has a well-defined Type. When variables of different types appear in an expression, the compiler is often able...