|
|
|
|
Lesson#3
|
Arrays and Pointers
|
|
|
|
Chapter 3
Arrays................................................................................................................................
2
Subscripts start at
zero......................................................................................2
Array variables as
parameters...........................................................................2
Operator Precedence
........................................................................................3
Initializing array
elements..................................................................................3
Multi-dimensional arrays
...................................................................................3
Array of C-strings
..............................................................................................4
Function Pointers
..............................................................................................4
Define a Function
Pointer..................................................................................5
Summary
..............................................................................................................5
Tips
.......................................................................................................................5
Arrays and Pointers 2
Arrays
An array is a collection of elements of same type. An array stores many
values in
memory using only one name. "Array" in programming means approximately the
same
thing as array, matrix, or vector does in math. Unlike math, you must
declare the array
and allocate a fixed amount of memory for it. Subscripts are enclosed in
square brackets
[]. Arrays are essentially sequential areas of memory (i.e. a group of
memory addresses).
However, we do not keep track of the whole array at once. This is because we
only have
a limited size of data to work with.
Subscripts start at zero
Subscript ranges always start at zero.
float x[100];
• first element of array is
x[0]
• last element of array is x[99]
Array variables as parameters
When an array is passed as a parameter, only the memory address of the array
is passed
(not all the values). An array as a parameter is declared similarly to an
array as a variable,
but no bounds are specified. The function doesn't know how much space is
allocated for
an array.
One important point to remember is that array indexes start from 0. Let’s
say our array
name is a of 10 ints, its first element will be a[0] while the last one will
be a[9]. Other
languages like Fortran carry out 1-based indexing. Due to this 0 based
indexing for arrays
in C language, programmers prefer to start loops from 0.
Arrays can also be multi-dimensional. In C language, arrays are stored in
row major order
that a row is stored at the end of the previous row. Because of this storage
methodology,
if we want to access the first element of the second row then we have to
jump as many
numbers as the number of columns in the first row. This fact becomes
important when we
are passing arrays to functions. In the receiving function parameters, we
have to write all
the dimensions of the array except the extreme-left one. When passing arrays
to
Arrays and Pointers 3
functions, it is always call by reference by default; it is not call by
value as in the default
behavior of ordinary variables.
Operator Precedence
C contains many operators, and because of operator precedence, the
interactions between
multiple operators can become confusing. Operator precedence describes the
order in
which C evaluates expressions
For example, ( ) operator has higher precedence then [ ] operator.
The following table shows the precedence of operators in C. Where a
statement involves
the use of several operators, those with the lowest number in the table will
be applied
first.
Initializing array elements
float x[3] = {1.1, 2.2, 3.3};
float y[] = {1.1, 2.2, 3.3, 4.4};
Initializing an array can be taken place in many ways. In the first line of
code, we are
declaring and initializing an array having three elements in it. In the
second array, we are
initializing and declaring an array of 4 elements. Note that we have not
specified the size
of the array on the left side of assignment operator. In this case, the
compiler will itself
calculate the dimension or the size of the array after counting the
initializer given in
parenthesis, and will declare an array of corresponding size.
Multi-dimensional arrays
The number of dimensions an array may have is almost endless. To add more
dimensions
to an array simply add more subscripts to the array declaration. The example
below will
show how this can be done.
int provinces [50];
// This will declare an one dimensional array of 50 provinces.
int provinces[50][500];
// This will declare a two dimensional array of 50 provinces each including
500 cities.
int provinces[50][500][1000];
// This will declare a three dimensional array.
When using this n-dimensional array, each item would be having a unique
position in
memory. For example to access the person in the second province, third city,
eighth
home, and the first person in the home the syntax would be:
Arrays and Pointers 4
provinces [2][3][8][1] = variable;
The size of the array would be very large. You can calculate the amount of
memory
required by multiplying each subscript together, and then multiplying by the
size of each
element. The size for this array would be 50 x 500 x 1000 x 4 x 2 bytes =
200,000,000
bytes of memory, or 190.73 megabytes, which would be unacceptable on today's
computers.
Array of C-strings
An array of C-strings is an array of arrays. Here is an example.
char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
In the above days array each element is a pointer to a string, and they
don't have to be of
the same size. For example,
char * greetings[] = {"Hello", "Goodbye",
"See you later"};
Function Pointers
Arrays and Pointers 5
Function Pointers are pointers, i.e. variables, which point to the address
of a function.
You must keep in mind, that a running program gets a certain space in the
main-memory.
Both, the executable compiled program code and the used variables, are put
inside this
memory. Thus a function in the program code is, like e.g. a character field,
nothing else
than an address. It is only important how you, or better your
compiler/processor, interpret
the memory a pointer points to.
int (*f1)(void); // Pointer
to function f1 returning int
Define a Function Pointer
As a function pointer is nothing else than a variable, it must be defined as
usual. In the
following example we define two function pointers named ptr2Function. It
points to a
function, which take one float and two char and return an int.
// define a function pointer
int (*pt2Function) (float, char, char);
Tips
• Arrays should be used carefully since
there is no bound checking done by the
compiler.
• Arrays are always passed by “reference” to
some function
• The name of the array itself is a constant
pointer
• Use function pointer only where it is
required.
Summary
The importance and use of Arrays should be very clear till now. Arrays are
basically a
data structure that is used to so store homogenous or same type of data in
it. A very
useful thing which we have analyzed here is that when we pass the name of
the array as
an argument to some function, then only the memory address of array is
passed as
parameters to the function and not all the values of an array are passed. In
the end we
have mentioned what the function pointers are? That is such pointers which
points to the
address of the function.
|
|
|
|