C Essentials basics | Page 7

Not Available
error in whatever
thing follows the struct declaration.
struct fraction {
int numerator;
int denominator;
};// Don't forget the semicolon!
This declaration introduces the type struct fraction (both words are required) as a
new type. C uses the period (.) to access the fields in a record. You can copy two records
of the same type using a single assignment statement, however == does not work on
structs.
struct fraction f1, f2;// declare two fractions
f1.numerator = 22;
f1.denominator = 7;
f2 = f1;// this copies over the whole struct
Arrays
The simplest type of array in C is one which is declared and used in one place. There are
more complex uses of arrays which I will address later along with pointers. The following
declares an array called scores to hold 100 integers and sets the first and last elements.
C arrays are always indexed from 0. So the first int in scores array is scores[0]
and the last is scores[99].
int scores[100];
scores[0] = 13;// set first element
scores[99] = 42;// set last element

160scores Index129913Someone else’s memory
off either end of the
array — do not read or
write this memory.There is space for
each int element in the scores array —
this element is
referred to as
scores[0].-56732254142These elements
have random
values because the
code has not yet
initialized them to
anything.The name of the array refers to the
whole array. (implementation) it
works by representing a pointer to the
start of the array. It's a very common error to try to refer to non-existent scores[100] element. C does
not do any run time or compile time bounds checking in arrays. At run time the code will
just access or mangle whatever memory it happens to hit and crash or misbehave in some
unpredictable way thereafter. "Professional programmer's language." The convention of
numbering things 0..(number of things - 1) pervades the language. To best
integrate with C and other C programmers, you should use that sort of numbering in your
own data structures as well.
Multidimensional Arrays
The following declares a two-dimensional 10 by 10 array of integers and sets the first and
last elements to be 13.
int board [10][10];
board[0][0] = 13;
board[9][9] = 13;
The implementation of the array stores all the elements in a single contiguous block of
memory. The other possible implementation would be a combination of several distinct
one dimensional arrays -- that's not how C does it. In memory, the array is arranged with
the elements of the rightmost index next to each other. In other words, board[1][8]
comes right before board[1][9] in memory.
(highly optional efficiency point) It's typically efficient to access memory which is near
other recently accessed memory. This means that the most efficient way to read through a
chunk of the array is to vary the rightmost index the most frequently since that will access
elements that are near each other in memory.

17
Array of Structs
The following declares an array named "numbers" which holds 1000 struct
fraction's.
struct fraction numbers[1000];
numbers[0].numerator = 22;/* set the 0th struct fraction */
numbers[0].denominator = 7;
Here's a general trick for unraveling C variable declarations: look at the right hand side
and imagine that it is an expression. The type of that expression is the left hand side. For
the above declarations, an expression which looks like the right hand side
(numbers[1000], or really anything of the form numbers[...]) will be the type
on the left hand side (struct fraction).
Pointers
A pointer is a value which represents a reference to another value sometimes known as
the pointer's "pointee". Hopefully you have learned about pointers somewhere else, since
the preceding sentence is probably inadequate explanation. This discussion will
concentrate on the syntax of pointers in C -- for a much more complete discussion of
pointers and their use see http://cslibrary.stanford.edu/102/, Pointers and Memory.
Syntax
Syntactically C uses the asterisk or "star" (*) to indicate a pointer. C defines pointer types
based on the type pointee. A char* is type of pointer which refers to a single char. a
struct fraction* is type of pointer which refers to a struct fraction.
int* intPtr;// declare an integer pointer variable intPtr
char* charPtr;// declares a character pointer --
// a very common type of pointer
// Declare two struct fraction pointers
// (when declaring multiple variables on one line, the *
// should go on the right with the variable)
struct fraction *f1, *f2;
The Floating "*"
In the syntax, the star is allowed to be anywhere between the base type and the variable
name. Programmer's have their own conventions-- I generally stick the * on the left with
the type. So the above declaration of intPtr could be written equivalently...
int *intPtr;// these are all the same
int * intPtr;
int* intPtr;
Pointer Dereferencing
We'll see shortly how a pointer is set to point to something -- for now just assume the
pointer points to memory of the appropriate type. In an expression, the unary * to the left
of a pointer dereferences it to retrieve the value it points to. The following drawing shows
the types involved with a single pointer pointing to a struct fraction.

18
struct fraction* f1;f1722denominatornumeratorstruct fraction*struct
Continue reading on your phone by scaning this QR Code

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