Introduction to programming in C: A step by step guide to learn C programming by Paul Elias

Introduction to programming in C: A step by step guide to learn C programming by Paul Elias

Author:Paul, Elias [Paul, Elias]
Language: eng
Format: epub
Published: 2020-06-19T16:00:00+00:00


Chapter 9

Pointers

A pointer is a variable that contains a memory address as a value. This address corresponds It usually sets the address of another variable in memory. It is then said that the pointer points to that variable . The pointed variable can be of any elementary type, structured or even another pointer.

Pointers are a fundamental part of the C language and are basic to understanding all the power and flexibility offered by language. They are especially important in programming at low level, where the computer memory is directly manipulated. Some of the advantages that provided by the use of pointers in C are:

They are the only way to express some operations.

Its use produces compact and efficient code.

They are essential for passing parameters by reference to functions.

They have a strong relationship with the efficient handling of tables and structures.

They allow you to perform dynamic memory allocation operations and manipulate memory structures.

dynamic data.

Finally, the reader should be warned that pointers are traditionally the most difficult part of C to understand. They must also be used with great caution, since by allowing direct manipulation computer memory can cause program failure. These failures are usually quite difficult to locate and fix.

9.1 Declaration and allocation of addresses

In the declaration of pointers and the subsequent allocation of memory addresses to them, respectively use the unary operators * and &. The operator & allows to obtain the address which occupies a variable in memory . For its part, the indirect operator * allows obtaining the content of an object pointed to by a pointer .

© The authors, 2000; © Edicions U PC, 2000.

Page 85

9.1. Declaration and allocation of addresses

76

9.1.1 Declaration

The pointer variable declaration also uses the operator *, which is applied directly to the variable to which it precedes. The format for the declaration of pointer variables is as follows: data type * variable name pointer;

Note that a pointer must be associated with a specific data type. That is, it cannot be assigned the address of a short int to a pointer to long int. For example: int * ip;

declares a variable named ip that is a pointer to an object of type int. That is, ip will contain memory addresses where integer values will be stored.

There should be no mistake in declaring multiple pointers using a single *. For example: int * ip, x;

declares the variable ip as a pointer to an integer and the variable x as an integer (not a pointer to a whole).

The data type used in the declaration of a pointer must be of the same data type as the possible variables that the pointer can point to. If the data type is void, it is defined a generic pointer so that its implicit data type will be that of the variable whose address is assign. For example, in the following code, ip is a generic pointer that throughout the program

points to objects of different types, first to an integer and then to a character.

void * ip;

int x;

char car;

. . .

ip



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.