C language for MSX – lesson 4

da | Dec 17, 2023 | C language | 0 comments

C language for MSX

Column by Nicola Brogelli (Part 4)

Pointers

In this handout we will mention what pointers are. This information will be useful for subsequent handouts where we will talk about pointer arithmetic related to arrays, how to pass parameters by reference, etc.

References to pointers

From the fundamental types we encountered in the first handout, other data types can be derived. For example, from an int type we can derive a pointer to int and so on for all other types. Now we can ask ourselves what a pointer is. We can say that a variable belonging to this type is an object whose value is a memory address. So a pointer object has as its value the address of another object or a function (in this handout we will not deal with function pointers).

The simplest form of declaring a pointer is as follows:

variable type *variable_name

In the following example we can see the declaration of pointer type variables.

int *p;

double *d;

float *f;

We can assign values to a pointer, if we assign 0 or NULL to it, we will obtain a null pointer. The null pointer does not refer to any object and therefore attempting to dereference it will lead to a run-time error. Or we could assign the memory address of a variable to the pointer. This is possible through the use of the & operator (address operator). The & operator is unary and prefix, it returns the memory address of its operand. In the following example we declare a variable of type int and assign it the value 5, then we declare a variable pointer to int and at the same time assign it the memory address of i.

int i = 5;

int *pi = &i;

To access a pointer variable, use the * operatordeferential or indirection). This operator is also unary and prefixed and returns the value of the variable pointed to by the pointer.
The following example prints the value of the variable i.e. 5.

printf(“%d”, *pi);

In some particular cases we may want to specify that a given pointer must not be used to modify the object it points to, this we could achieve via the keyword const

int i;

const int *pi = &i;

Following this declaration, all subsequent instructions that attempt to change the value i using the pointer pi will be reported as an error.

*pi = 5; // not legal

Pointers to void

We said that the declaration of a pointer specifies the type of the referenced object. In some cases it can be useful if a variable can contain pointers to different object types. In this case the variable is defined as a pointer to void. Before being dereferenced, a void pointer must be cast to the appropriate type. With a simple example we see how a void pointer is used to access first an integer and then a character.

int i;

int *pi = &i;

int c;

int *pc = &c;

void *vp;

vp = pi;

*(int*)vp = 3;

vp = pc;

*(char*)vp = 'c';

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

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