Advanced C topics – Part2

Some of the things which could not fit into the previous post will be explained here.

In the previous post related to advanced C topics we were discussing about comparaison between arrays and pointers, how is an array of characters different than a pointer to an array of characters, how can arrays be passed as parameters and how you can avoid being tricked by the way bi-dimensional arrays are passed as parameters. In this post I will try to outline how can an array be returned as a value from a function and what’s the use of pointers to functions.

Let me start with the first item: how to return arrays from functions. Just have a look at the following code snippet:

char* function (int n)
char buffer[200];
return buffer;

Read more of this post


Advanced C topics – Part1

I just want to touch in this post some of the things which are generally considered to be advanced C topics. I am referring here to things like: differences between pointers and arrays, arrays passed as parameters and returned as value from functions, and pointers to functions.

Can we pass an array as a function parameter in C language? Isn’t quite strange when we see in C programs something like this:

void function (int a[], int n);

How can one pass an entire array as a parameter to a function? How many parameters does the corresponding function accept, two or many?
The answer is quite straightforward: you are passing in fact just an address as a parameter. This is a pointer to the first element of the array. It is the old discussion about different ways of how to refer to an array or to an element of it (if you define it like this int a[] you can refer to it like this int *a; and refer to its elements with *(a+2) – third element in the array; or you can refer like this a[2] – 3’rd element).
Please be aware when passing arrays not to overflow the stack. This is the danger that arrays passed as parameters may involve: compiler never knows how big the array will be, because it will replace the array declaration with a pointer so it will reserve space just for a pointer.

Actually when defining an array the compiler automatically generates a pointer to its first element, it is something like this:

void function (int &a[0], int n);

Read more of this post

sizeof() operator – not a trivial thing

sizeof() operator … Remember it?
That one mostly used when dynamically allocating memory:

int *pointer; /*pointer to type int, used to reference our allocated data*/
pointer = malloc(sizeof(int) * 10);

Some things about it:

– it returns the size in bytes of any primitive or user-defined variable
– it always returns something positive, as it is defined it returns an unsigned size_t type
– if a type is used like in this example
its argument needs to be parenthesized
-if just variable’s name is used parenthesis can be omitted

char c;
sizeof c
– in the C standards preceding C99 it was a compile-time operator, meaning that it calculated only sizes defined at compilation, it could not calculate something like this
char c[];

Read more of this post

const qualifier in C language

One thing which I considered to be easy enough not to be taken into consideration but few days ago raised some real issues: const qualifier.

So… what to say about const? Usually the first answer a C programmer gives to this question is const indicates something that cannot change. In reality this is far from being an exhaustive definition of const and it is not even 100% correct.

There are three topics here and just let me take them in order, there are const variables (which is quite straightforward), there are also const functions and const pointers (… and const references). In case of last two, things are complicating a little bit.

Let’s just go back and just slightly correct the assumed definition of const. It is better to say a const variable  is a compile-time fixed variable. By qualifying a variable as const you’re getting a guarantee from the compiler that the variable won’t change through that name.

const int a = 1;
a = 2;

The code above will issue an error (one thing to notice is that const data types always must be initialized) but the one below will not

const int a = 1;
int *b = &a;
*b = 2;

As you may see an object or a data stored somewhere in the memory, can be referenced via an expression.

Here you may find an article exhaustively treating this subject which I strongly recommend you to read. There is a key sentence  highlighted on the first page:

The expression you use to refer an object differs from the object itself.

… and it continues

Sometimes this distinction matters.

Read more of this post