Menu
Accueil
Forum
Liste des membres
Livre d'or
S'inscrire
Maths
Modules pour CrazyCMS
Consommation électricité
Me contacter
Espace Membre
Statistiques
livre_dor
Pas de commentaires dans le livre d'orRetour au menu
Lesson 5 –
Arrays, pointers and strings
1)
Arrays
2) Pointers
3) Strings
This part of c language is very important because you cannot make a program
without using an array. Indeed the arrays are able to deal great data. Your
program is more readable and also more possible. The strings are more specific
and you can use a pseudo-constant to display text instead of using a string
variable. Yet the strings are useful when you want to make a game. For instance,
when you want to display a score, the number of lives, … you had to use a string
variable. The “DrawStr” function uses only a character input and you cannot
display directly a number unlike of “printf” function. On the other hand, the
pointers are specific to c language. The pointers are very powerful and are able
to decrease the memory and increase the performance of your program. I deal with
these three notions because they are equivalent; the writing changes only.
1) Arrays
An array gathers several variables with the same type. I will explain how an
array works later with the pointers. The arrays are very useful to deal with an
amount of data because each line of your array is a variable and each variable
has a number instead of a name. What means you can use another variable so as to
use the variable of the line of your array. Thus, you can easily use the array
within a loop and deal with a great amount of data. Now we can see how we can
use the arrays.
The array works like a variable, you must declare and assign an array to be able
to use. The syntax is:
Declaration:
Type name [the number of lines]
Assignment:
Name [the number of the line] = value
In fact, in c language, the arrays always begin from 0. Of course the last
number of row is (the number of lines – 1).
Now to illustrate this new notion a brief example:
// C Source File
// Created 01/01/2003; 17:48:11
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
int array1[4];
short array2[17];
short a = 22;
array1[0] = 21;
array1[1] = a;
array1[2] = array1[0] + 2;
a = 3;
array1[a] = 24;
clrscr();
for (a = 0; a < 4; a++)
printf ("%d ", array1[a]);
for (a = 0; a < 17; a++)
array2[a] = a;
ngetchx();
}
There is another type of arrays: the matrix; in fact you have rows and colons.
The syntax is :
Declaration:
Type name[the number of rows] [the number of colons]
Assignment:
Name[the number of the row] [the number of the colon] = value
The matrix gathers several arrays. I have done a small graphics to see, it is
easier to imagine and understand the working of a matrix.
1 | 2 | . | . | . | j | m | |||||||
1 | |||||||||||||
2 | |||||||||||||
. | |||||||||||||
. | |||||||||||||
. | |||||||||||||
i | |||||||||||||
n |
Type matrix [n][m]
matrix [i][j] = black box
As I discussed previously, the arrays are especially powerful to deal with an
amount of data. Let us see an example to sort an array!
// C Source File
// Created 01/01/2003; 19:00:30
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
short array[13];
short a,z;
short aux;
randomize ();
clrscr();
printf ("An random array:\n");
for (a = 0; a < 13; a++) {
array[a] = random (10); // 0 <= short <= 9
printf ("%d ", array[a]);
}
for (a = 0; a < 12; a++) {
for (z = a + 1; z < 13; z++) {
if (array[a] > array[z]) {
aux = array[z];
array[z] = array[a];
array[a] = aux;
}
}
}
printf("\n\nThe sorted array:\n");
for (a = 0; a < 13; a++)
printf ("%d ", array[a]);
ngetchx();
}
Do not worry with the new functions: randomize() and random(). I will explain
you later in details. For this moment, you should know only that “random(number)”
function gives you a number between 0 and number; exactly: 0 <= random(number)
<= number –1.
On the other hand, the two loops that are able to sort the array are more
complex to understand. There are several ways to sort an array but this way is
for me the best on the speed and the size of memory. So as to understand how the
sort works, you can see the bellow diagram:
7 | 2 | 1 | 5 | 3 |
2 | 7 | 1 | 5 | 3 |
1 | 7 | 2 | 5 | 3 |
1 | 2 | 7 | 5 | 3 |
1 | 2 | 3 | 5 | 7 |
2) Pointers
It is a specificity of c language. The pointers are very powerful and now I am
going to explain only the basis. In the next lessons, I explain more interesting
uses: procedures, functions, files, …
A pointer is a variable which the main aim is to remember the address of an
area. Unlike a variable which remembers a value. This address is a hexadecimal
value which points on an area of memory. In fact, you can get the value of this
area and after you can use a pointer like a variable. Yet, I’ve never told you
but you can also get the address of a variable, even if the main aim of a
variable is its value. The declaration and the assignment of a pointer is not
harder than with a variable. You should remember only that you work on the
address. Here is an example.
// C Source File
// Created 28/01/2003; 18:40:15
#define USE_TI89 // Compile for TI-89
#define USE_TI92PLUS // Compile for TI-92 Plus
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
int *pointer1;
int *pointer2;
int *pointer3;
int a = 13;
int z;
pointer1 = &a;
pointer2 = pointer1;
*pointer2 += 4;
pointer3 = &z;
z = 17;
clrscr();
printf ("pointer1: %d\npointer2: %d\npointer3: %d\n", *pointer1, *pointer2, *pointer3);
ngetchx();
}
To declare a pointer, you must define the type of your pointer and its name like
a variable but you add an ‘*’ so as to inform the compiler. It is not harder!
;-) After you do not initialize a pointer with a value but with an address of a
memory address. This address is one of a variable or one of another pointer.
When you do not use a ‘*’, it means you work on the address of pointer or else
you work on the value of the address of your pointer. Remember that with
pointers you work on the address and you can get the value of the pointed area.
3) Strings
We are going to see several functions
to work with the strings. Indeed, the strings are a specific type and you cannot
use the usual assignment acronym,
it creates errors which are hard to understand and it is very easy to make
mistakes!
The declaration of your string depends on the using of your string: variable or
constant. If you want to use only a constant string, you can declare without
giving the size of your string. If you want to use a string like a variable, you
have to tell compiler the size of your string and especially you mustn’t use the
‘=’ acronym! The syntax is:
char name[] = “data”;
char name[value];
with:
‘name’: the name of your string
‘data’: your string of characters
‘value’: the size of your string
After you want to initialize the string variable, you must use a specific
function and not the ‘=’ acronym! This function is ‘strcpy’ (copy the string)
and here the syntax:
strcpy (string1, string2);
with:
‘string1’: the name of the string that you want to initialize
‘string2’: the name of the string which you assign at the string: ‘string1’ or
you can replace ‘string2’ by “data” (a constant string of characters)
If you want to concatenate two strings, in other words, to add two strings, you
should use the ‘strcat’ function whose the syntax is:
strcat (string1, string2);
In fact, ‘string2’ can be a constant string of characters. The result is
equivalent to: ‘string1 = string1 + string2’ but you cannot use the ‘=’ acronym!
See another lesson
Retour au menu