User Tools

Site Tools


handbook:handbook:programing3

Advanced concept

In this part of this handbook, we shall talk about advanced concepts in the C programming language. We already discussed the basics of this language and gave a simple overview of what variables, functions and standard intput/output is. Now the time has come for some more difficult examples and concepts.

Variables

Yes, we go back to variables again. Remember, we’ve only talked about the four basic types of variables so far: int, double, float and char. But there are few more types of variables, which give extra usability and power to the C language.

Pointers

Pointers are variables which hold memory addresses instead of values. They are used to point to a specific memory address. Depending of what kind of adress we would like to store, there are integer, character, float and other types of pointers. Declaring an integer pointer:

int* ptr;

“int” ,of course, says that it is integer pointer.The little ‘*’ informs the compiler that this is not a regular integer, it is a pointer to an integer’s adress. “ptr” is the name of the pointer. Because it has no value, it is called “null” pointer. Now, if we like to store some integer address in our ptr:

ptr = &number;

Here we put the address of the integer number into ptr. From now on, ptr points to number. If we would like to get the value of the pointer, not the memory adress:

printf("The value of ptr is: %d\n", *ptr);

The ‘*’ sign is called “dereferencing operator”, and we can use it to assign values to pointers as well:

*ptr = 8;

Arrays

Arrays are simply container of more then one variable of the same type. There can be array of integers, array of characters and so on. Declaring an array:

int integer_array[100];

This is an example of integer array. Its name is “integer_array” and it can contain 101 integer. How do we know that? Well, the number between the brackets, 100 in this case, is called “index”. The index refers to the ordinary number of all variables stored into the array. But, the array begins to count its variables from 0. So, the first member of the array could be reached as:

integer_array[0];

The second, you might already guess, is integer_array[1] and so on. The last member of the array is integer_array[100]. If you use a number greater then 100 , assuming that you’ve already declared array as “integer_array”, the compiler will abort the compilation with error. Of course, you can assign values to any of the array members:

integer_array[4] = 5; //this is the fifth member

Initializing an array:

int array[3] = {1,2,3,4};
char chars[2] = {‘a’,’b’,’c’};

Strings

The best way to represent string is to show a real life example. For example, some name, like “John” is a string in C, and other programming languages as well. Look at string like an array of characters. But, C strings are also called null-terminated strings, because the last character of every string is ‘\0’. That indicates that the end of the string is there. For example, a string containing the name John would contain:

‘J’,’o’,’h’,’n’,’\0’

To simplify the work with strings, a number of string libraries and functions exist. I will give a short overview now:

char* strcpy (char* s1,const char* s2) Copies string s1 into the character array string s2 and returns the value of s1. strncpy() is a fellow function.

char* strcat (char* s1, const char* s2) Appends the string s1 to the end of the char array s2. The value of s1 is returned. strncat() also exists.

int strcmp (const char* s1, const char* s2) Compares the string s1 to the string s2. Returns 0 if they are same, <0 if s1 <s2, >0 if s1 > s2. strncmp() exists.

size_t strlen(const char* s) Determines the length of the string s. Returns the number of characters before ‘\0’. Please note that the character pointers (char*) are constant variables, their value cannot be changed. Of course, there are lot of other C string functions, which might be useful when working with strings.

Loops

What are loops? Well, loops are a way of reusing the same code, cycling through it again and again. During your programming challenges, you will find it handy to use a lot of forms of looping. In C language, there are two types of loops:

  • while
  • for

While

The while loop is repeating the code within the brackets until the condition is satisfied. Example of while loop:

while ( 1=1 ) /*while the condition is true*/
{
printf ("Again\n");
}

As you might already have guessed, this is a never-ending loop. Although it is not a very good example of using the while loop, it expresses the condition and the code which are linked in a very, obvious, way. It is never-ending loop, because the condition “1=1” will always be true.

It is also possible to use the do while statement. It is done in the following way:

do
{
     printf ("Again\n");
} while ( 1=1 );

The only difference between the while and do while statements is , that in do while the body of the code is executed at least once. Even if the condition is wrong, it will be executed once.

For

In some cases, the for loop can be more useful then while. In order to explain it, we will give an example first:

for (counter = 0; counter < limit; counter++)
{
printf("The value of counter is %d\n",counter);
}

The for loop is built from three parts. First, the initialization part, and that is counter = 0. It initializes the value of the counter, which determines how many times the code within the brackets will be repeated. Then, comes the condition part, counter < limit. It is similar to while loop, when this condition is satisfied, the looping will stop. And, at last, is the iteration part, which is performed every time a loop will end. In our case, we increment the value of the counter. It is possible, however, to insert multiple statements in each part, delimiting them with a comma.

for (counter = 0; counter < limit; counter++, other_var--)

Of course, using the while loop, instead of the for loop is possible, but in this order:

initialization;
while (condition)
{
code body;
iteration;
}

Structures

In the C programming language, the structures are used to group variables or functions with the same purpose. They are used to create cleaner, easy understandable and practical code. They are defined in the following way:

typedef struct employee
{
int age;
char adress[100];
int working_hours;
}Employee;

This is an example structure, referring to a employee record. The fields inside are not so important to us now, since they are simple variable, they can be easily modified and they are very structure type dependant. The structure is named employee. The field are surrounded by brackets. The last “Employee” word is not a necessity, but it is a standard practise. The keyword “typedef” is used to define a new type, so every variable of this structure will be of the type “struct employee”, just like working_hours is an integer. You can create structure variables by typing:

struct employee john; 

or:

Employee john;

Then, you can modify specific structure types with:

john.working_hours = 5; 

You can reach any of the other structure members on the same way as we did with working_hours.

Of course, we can create pointers, which will point to a structure:

struct empoyee *amelia;

But, the syntax will change for a bit:

amelia -> working_hours = 6;

Unions

Unions are another way to group code. The syntax of the union is very similar to the one of the structures. Unions and structures are in a very close relation. Every union can contain a structure and vice-versa. Example is in order:

typedef struct employee
{
	int age;
	union
	{
		char street[100];
		int strnumber;
	} address;
	int working_hours;
} 

So, we create a variable:

employee john;

And we can reach the members inside:

john.age = 45;
john.address.strnumber = 3;
handbook/handbook/programing3.txt · Last modified: 2010/04/15 21:18 (external edit)