unsigned char Mystring[] = "COMP";
and
unsigned char Mystring[] = {'C', 'O', 'M', 'P', '\0'};
In C programming language, we can also declare arrays with multiple dimensions. One-dimensional arrays are usually called vectors, and two-dimensional arrays are called matrices. A two-dimensional array is declared by specifying the data type of the array, the array name, and the size of each dimension. In the following example, a two-dimensional array named P is created having three rows and four columns. Altogether, the array has twelve elements. The first element of the array is P[0][0], and the last element is P[2][3]. The structure of this array is shown below:
P[0][0] | P[0][1] | P[0][2] | P[0][3] |
P[1][0] | P[1][1] | P[1][2] | P[1][3] |
P[2][0] | P[2][1] | P[2][2] | P[2][3] |
Elements of a multidimensional array can be specified during the declaration of the array. In the following example, two-dimensional array Q has two rows and two columns, its diagonal elements are set to 1, and its nondiagonal elements are cleared to 0:
unsigned char Q[2][2] = { {1,0}, {0,1} };
3.1.15 Pointers
Pointers are an important part of the C language, as they hold the memory addresses of variables. Pointers are declared in the same way as other variables, but with the character (“*”) in front of the variable name. In general, pointers can be created to point to (or hold the addresses of) character variables, integer variables, long variables, floating point variables, or functions (although mikroC currently does not support pointers to functions).
In the following example, an unsigned character pointer named pnt is declared:
unsigned char *pnt;
When a new pointer is created, its content is initially unspecified and it does not hold the address of any variable. We can assign the address of a variable to a pointer using the (“&”) character:
pnt = &Count;
Now pnt holds the address of variable Count. Variable Count can be set to a value by using the character (“*”) in front of its pointer. For example, Count can be set to 10 using its pointer:
*pnt = 10; // Count = 10
which is the same as
Count = 10; // Count = 10
Or, the value of Count can be copied to variable Cnt using its pointer:
Cnt = *pnt; // Cnt = Count
In C language the name of an array is also a pointer to the array. Thus, for the array:
unsigned int Total[10];
The name Total is also a pointer to this array, and it holds the address of the first element of the array. Thus the following two statements are equaclass="underline"
Total[2] = 0;
and
*(Total + 2) = 0;
Also, the following statement is true:
&Total[j] = Total + j
In C language we can perform pointer arithmetic which may involve:
• Comparing two pointers
• Adding or subtracting a pointer and an integer value
• Subtracting two pointers
• Assigning one pointer to another
• Comparing a pointer to null
For example, let’s assume that pointer P is set to hold the address of array element Z[2]:
P = &Z[2];
We can now clear elements 2 and 3 of array Z, as in the two examples that follow. The two examples are identical except that in the first example pointer P holds the address of Z[3] at the end of the statements, and it holds the address of Z[2] at the end of the second set of statements:
*P = 0; // Z[2] = 0
P = P + 1; // P now points to element 3 of Z
*P = 0; // Z[3] = 0
or
*P = 0; // Z[2] = 0
*(P + 1) = 0; // Z[3] = 0
A pointer can be assigned to another pointer. In the following example, variables Cnt and Tot are both set to 10 using two different pointers:
unsigned int *i, *j; // declare 2 pointers
unsigned int Cnt, Tot; // declare two variables
i = Cnt; // i points to Cnt
*i = 10; // Cnt = 10
j = i; // copy pointer i to pointer j
Tot = *j; // Tot = 10
3.1.16 Structures
A structure can be used to collect related items that are then treated as a single object. Unlike an array, a structure can contain a mixture of data types. For example, a structure can store the personal details (name, surname, age, date of birth, etc.) of a student.
A structure is created by using the keyword struct, followed by a structure name and a list of member declarations. Optionally, variables of the same type as the structure can be declared at the end of the structure.
The following example declares a structure named Person:
struct Person {
unsigned char name[20];
unsigned char surname[20];
unsigned char nationality[20];
unsigned char age;
}
Declaring a structure does not occupy any space in memory; rather, the compiler creates a template describing the names and types of the data objects or member elements that will eventually be stored within such a structure variable. Only when variables of the same type as the structure are created do these variables occupy space in memory. We can declare variables of the same type as the structure by giving the name of the structure and the name of the variable. For example, two variables Me and You of type Person can be created by the statement:
struct Person Me, You;
Variables of type Person can also be created during the declaration of the structure as follows:
struct Person {
unsigned char name[20];
unsigned char surname[20];
unsigned char nationality[20];
unsigned char age;
} Me, You;
We can assign values to members of a structure by specifying the name of the structure, followed by a dot (“.”) and the name of the member. In the following example, the age of structure variable Me is set to 25, and variable M is assigned to the value of age in structure variable You:
Me.age = 25;