perform the operations.
Declarations
2-8 C Language Overview © Copyright 2003 by COSMIC Software
2
Pointers
The C language allows more complex types than the simple numerical
types. The first one is the pointer which allows for simple handling of
addresses. A pointer is a variable which contains an address. In order
to know what to do with that address, a type is associated with the
pointer.
A pointer takes the type of the object pointed at by the pointer value. A
pointer in C is associated with the operator
*, which is used to declare a
pointer and to designate the object pointed at by the pointer. A pointer
allows access to any location of the processor memory without any con-
trol other than any hardware control mechanism. It means that a pointer
is as convenient as it is dangerous. In general, the C language does not
verify anything at execution time, producing good efficiency, but with
the same security as assembler, meaning none. All values are possible,
but the C language reserves the value zero to designate an invalid
pointer, although nothing stops a program accessing memory at address
zero with a pointer. The size of a pointer in memory depends on the tar-
get processor. Most of the small microprocessors address 64K of mem-
ory and use 16 bit addresses. A pointer is then equivalent to an
unsigned short. Some processors allow several memory spaces with
various addressing mechanisms. To allow the best efficiency, the com-
piler supports three different pointer types by defining a size attribute
along with the pointer:
a
tiny pointer is a one byte address (unsigned char)
a
near pointer is a two byte address (unsigned short)
a
far pointer is a four byte address (unsigned long)
The compiler allows the user to select a memory model to choose a
default size when no attribute is explicitly specified. In most of the
cases, the near pointer will be used by default. When the addressing
space is not large enough to hold the full application, a possible solution
is to use a bank switched mechanism to enlarge the physical memory
space, while keeping the same microprocessor and its logical memory
addressing capabilities. In that case, far pointers are used to hold a two
component address, consisting of a bank number and a logical address.
Bank switching is mainly used for functions and sometimes allowed on
data accesses depending on the target processor.
Declarations
© Copyright 2003 by COSMIC Software
C Language Overview 2-9
Arrays
The next complex type is common to several languages. An array may
be defined as a collection of several objects of the same type. All these
objects are allocated contiguously in memory and they may be accessed
with an index, designating the rank of an object within an array.
An array is defined with the type of its elements and a dimension. An
index always starts at zero in C. An index is applied to an array with the
[] operators. No control is done to check that the index is in the proper
range. It is possible to define an array of all the existing data types of C.
An array in C has only one dimension. Multidimensional arrays are
possible by creating arrays of arrays.
Structures
A structure may be defined as a collection of several objects of differ-
ent types. No index can be used as objects may have a different size.
Each object will be accessed individually by its name. Such a structure
member will be called a field. A structure is a convenient way of group-
ing together objects related to a specific feature. All the members are
allocated in memory in the order defined by the structure and contigu-
ously. Note that some processors have addressing restrictions and need
sometimes to align data in memory on even boundaries for instance.
The C compiler will respect these restrictions and will create holes if
necessary to keep alignment. This means that a structure may not be
allocated exactly in the same way depending on the target processor.
Most small microprocessors do not require any alignment. The size of a
structure in memory will be the sum of the size of each of its fields, plus
if necessary the size of the padding holes. A structure may contain spe-
cial fields called bitfields defining objects with a size smaller than a
byte. Such objects are defined with a bit size.
Unions
A union is a variant of a structure. In a structure, all the members are
individual objects allocated contiguously. In a union, all the members
refer to the same object, and are allocated at the same address. All the
members of a union are equivalent, and the size of a union will be the
size of its largest member. A union is a convenient way to save memory
space when a location may be used with different formats depending on
the context.
Declarations
2-10 C Language Overview © Copyright 2003 by COSMIC Software
2
Enumerations
An enumeration is an integer object defined by the list of its possible
values. Each element is an integer constant and the compiler will
choose the smallest integer type to implement such an object.
Functions
The C language defines a function as an object.
Continue reading on your phone by scaning this QR Code
Tip: The current page has been bookmarked automatically. If you wish to continue reading later, just open the
Dertz Homepage, and click on the 'continue reading' link at the bottom of the page.