C language for MSX – lesson 6

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

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 also allows you to define multidimensional arrays, in this case at least two indices are needed to identify a desired element.

type identifier [constant-expressio] [constant-expressio] ……

Although a multidimensional array is stored as a sequence of elements, it can be manipulated as an array of arrays. So to access an element of a multidimensional array we need to specify as many indices as there are dimensions.

In the next example we have the multidimensional array declaration number of integer type.

int numbers[3][4] = {{10,0,12,-1}, {7,-3, 2, 5}, {-5,-2, 2, 9}};

There are several methods for accessing a multidimensional array, the one using indices is certainly the simplest one and we can see it in the example below in line 1.

int a = numbers[1][2];
int b = *(numbers[1]+2);
int c = *(*(numbers+1)+2);
// in all three cases the result of the assignment is the value 2

It is however possible to access the multidimensional array via pointers, the syntax is certainly more complex and can be expressed as in the example on lines 2 and 3.

Recall that a two-index array like the one in the example ( numbers[3][4] ), occupies 3 × 4 "contiguous" positions in memory and therefore, in some ways, it could be considered as a one-dimensional array of length 12. I remind you that the order in which the elements of the array are stored follows the rule whereby the index the further to the left it grows more slowly. So in memory after the element numbers[0][0] we have numbers[0][1], Then numbers[0][2] and so on.

We can think of the array numbers[3][4] as a 3-element array, where each element points to a 4-element array. If we adopt this point of view it becomes clear that the name numbers or numbers[0] is a pointer to the first element of the two-dimensional array, i.e. to the first sub-array of length 4; Therefore numbers + 1 or numbers[1] points to the second sub-array of length 4 and so on.

The expression *(numbers + 1) is equal to the contents of the memory location it points to numbers + 1; this location contains the address of the second sub-array, therefore *(numbers + 1) it's not an array element but it's still a pointer. To be clearer it contains the address of the sub-array containing the values {7,-3, 2, 5}. And in the same way the expression is still a pointer *(numbers + 1) + 2, which is the address of the third element of the second sub-array.

And finally the expression *(*(numbers + 1)) dereference the pointer *(numbers + 1) returning the contents of the pointed memory location: this content corresponds to the first element of the second sub-array, which, in terms of indices, we can write numbers[1][0], and in the same way the expression *(*(numbers + 1) + 3) is equal to numbers[1][3].

I hope the handout is clear enough, in the following we will return to talking about arrays as a parameter of functions.

For those interested theMSX Italy Association created a R&D working group on C programming for MSX. For more info:associazioneMSXitalia@gmail.com

0 Comments

Submit a Comment

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

Linguaggio C per MSX – lezione 5

C language for MSX – lesson 5

C language for MSX Column by Nicola Brogelli (Part 5) Arrays and Array Pointers Arrays are sequences of variables of the same type that are stored consecutively in memory and which can be accessed using the same name...

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...