/dev/thomynet

developer places

... the C lab ...

(some thinks for C)


The ... c-lab ... contains:


Array-Routines

Compute the size of an array

Create an array with:
int int_array [] = { 5, 10, 13, 24};
The question is: Which size has the array int_array[]?
Answer:
int array_size = sizeof(int_array)/sizeof(int_array[0]);
int max_nr_in_array = array_size-1;
Remember: For example the size is 4 and index is from 0 to 3.

But:
If you give the array as an argument to a function, inside the function you can´t compute the arraysize!

Example:
Here you can look for an example programme or the same as html-output.

You can download the example (incl. html-output) and compile it with:

cc -Wall -O2 -o array1 array1.c
Start it with:
./array1
The output you can see here:
The sizeof(int) is: 4
The sizeof(array) is: 12
The sizeof(array[0]) is: 4
The sizeof(*array) is: 4
The address of array is: 0xbffff87c
The address of &array[0] is: 0xbffff87c
The address of &array[1] is: 0xbffff880
The address of array+1 is: 0xbffff880
The value of *array is: 1
The value of array[0] is: 1
The value of *array+1 is: 2
The value of array[1] is: 2
The number of elements in array is: 3
The number of elements in array is: 3
The address of array in prtfunc is: 0xbffff87c
The sizeof(array) in prtfunc is: 4
The sizeof(&array) in prtfunc is: 4
array[0] = 1 and has address: 0xbffff87c
array[1] = 2 and has address: 0xbffff880
array[2] = 3 and has address: 0xbffff884
The following thinks only work with a pointer to our array -
because array is a const pointer to first element and
can´t change [get an compiler error]!
Get this pointer with:
        int *a_p = array;
To increment the second element do:
         ++(*(++a_p))
New values are:
array[0] = 1 and has address: 0xbffff87c
array[1] = 3 and has address: 0xbffff880
array[2] = 3 and has address: 0xbffff884
To increment the first element do:
         ++(*a_p)
New values are:
array[0] = 2 and has address: 0xbffff87c
array[1] = 3 and has address: 0xbffff880
array[2] = 3 and has address: 0xbffff884

The best way for string arrays

Create an string array with:
const char *days[] = { "Monday", "Tuesday",
                       "Wednesday", "Thursday",
                       "Friday", "Saturday", "Sunday"};
Compute the size with descripe before and use the array.

Advantage of this way:
If you append or insert or delete an element you don´t must change your source!
 

Debug-Helper

Do you know the problem with many of printf´s? Insert, comment out, delete commentsigns, comment out ...
With some little helpers you can solve this ...
I have developed a little include-file for this purpose. The name of it is ts_debug.h. You must only include this in your source and have 4 new functions for debugging.
In my mind its should be enough comments in the include-file what you can find here as a html-output. A little example will comes up later (no time - sorry). If you are interested you can download the includefile with html-output as tgz.



Questions, ideas, comments please mail me at: linux@dev-thomynet.de