Archive for the 'C' Category

Pointers

Pointers are very common and powerfull parts of programming languages such as C and C++. If you have never encountered pointers before (you are probably from the world of Java or .NET) you may ask “Why?”. Well, if you know an object-oriented language you may notice how you can use the pointers to simulate behaviours similar to reference types. Like reference types pointers “point” to a specific object, but through its address in memory.

In C and C++ you declare and use a pointer this way:


int a = 20;
int *p = &a;

printf("%d", p);

*p = 30; //Manipulating the value (*p)

printf("%d", *p); // Will print 30 to the console.

printf("%d", p); // Will print the address which the pointer points to (p)

I will now explain this fairly simple piece of code. We declare an integer (a) and give it the value 20. Nothing strange so far. Then we declare a pointer to integer variable. int* means pointer to integer. It is being initialized with the address of the integer (a) we declared. The address operator is & (read as address of). Later we starts to manipulate the memory through the pointer. Just notice the difference between *p and p (commented in the code). As it is shown both p and a are referencing the same memory. The power of pointers! But when you are getting more experienced and you are working with more advanced stuff you have to take responsibility. That will I talk about later in this blogpost.

A native string type does not exist in C, but you can simulate one with arrays or pointers. Yes, pointers. Take a look at the following example where a pointer to a character is initated with a string.


char *str;
str = "Hello, World!";

//This is similar to:
char[] strAr = "Hello, Second World!";

As I said before it is almost like OOP with reference types. You can point to primitive types and structures, and even functions. Function pointers are similar to delegates in C#, though delegates are type-safe and function pointers not. These are declared and initiated as in this example:


int (*Operation)(int OpA, int OpB);

static int Add(int OpA, int OpB)
{
      return OpA + OpB;
}

static int Sub(int OpA, int OpB)
{
      return OpA - OpB;
}

int Main(void)
{
      Operation = Add;
      int r1 = (*Operation)(2, 3); //5

      Operation = Sub;
      int r2 = (*Operation)(3, 2); //1

      return 0;
}

With these two types of pointers you can do a lot of cool and advanced stuff. One of the many common uses of type pointers are allocation and reallocation of memory. You do like this:


int *ptr = malloc(sizeof (int));

The argument of malloc is the size of memory you want to allocate.

Because you are allocating the memory the program cannot manage it for you. That is to say that if your program is ending you will have to reallocate the memory you have used. The memory will remain allocated if you do not do so. That makes exception handling very important in your program. The There are certain functions to reallocate or free occupied memory. The most simple are free which takes a pointer as an argument. It will automatically free the memory the pointer is referencing to.


     free(ptr);

Because of the pointers, languages like C and C++ are good for system- and hardware programming. If you for instance want to write a driver for a specific device, that is manipulating the hardware, you use pointers because they are able to manipulate memory and through it the basic parts of the computer. Pointers make C nearly as powerfull as assembler (machine language), as close to the hardware you can get.

This was a short introduction to pointers. There are many of ways of using them. It takes a lot of practice to master them without messing stuff up although they are easy to declare.

I just want to add that C# does have pointers although they are not common to use. They are mostly used in P/Invoke (platform invocation) to unmanaged native code in i.e. the Windows API. These parts are abstracted away by the managed OOP classes in the .NET Framework. You can still do some pretty advanced stuff with pointers but you do not need to, or rather you are encouraged not to use them, because they considered are unsafe. They are thereby against the principles of the CLI (Common Language Infrastructure). Java does currently not have pointers (no instructions for them). Maybe they will add support in the future, but who knows.

Thanks for reading.

C Tutorial

Tutorial for beginners.

http://www.le.ac.uk/cc/tutorials/c/

Hello World! (C)


#include <stdio.h>

int main(void)
{
       printf("Hello, World!\n");
       return 0;
}


Pages

Categories