Monday, 16 January 2012

Pointers

One of the hard concepts that I encountered in C is pointers. However, pointers are very important and widely used in C (particularly for my case since I am using arrays) so I must understand how pointers work. According to Kernighan and Ritchie in their book “Programming Language in C”, a pointer is a variable that contains the address of a variable.

Here is an example how pointers work.

  1.  int * a; // define integer type pointer(int *) a
  2.  int b = 5;
  3.  a = &b; // & is get the address of the variable
  4. int d = *a; // * is same as int d = b


  1. Define the integer type pointer named a (In C, a is an int pointer which is stored in address 201)
  2. Define integer b and assign the value 5 to b (In C, b is 5 which is stored in address 301)
  3. a is pointing at the address of b (Now, a has the value of b’s address)
  4. * means getting the value of what a is pointing at. That is, a is pointing at b and the value of b is 5. Therefore, *a is 5. 5 is then assigned to d.

Here is the diagram of the example:





I will explain why pointers are important and useful later.

Saturday, 7 January 2012

Introduction to C

Currently, I am working on my Master thesis using R. My project has a huge data set with many parameters to estimate (over 2000) by using MCMC with complicated likelihood functions (6 layers of for loops). I wrote the code in R and finally I was able to run it. Unfortunately, 1 iteration took a day! Since I need to run it at least 10,000 times for it to get an answer, it would take about 27 years to get the result. No way, I don't want to wait 27 years to graduate! Fortunately, I heard the programming language C is comparatively faster than R and I am starting to learn how to write C.

Since R is written partially in C and partially in Fortran, C code can be called inside R. That is, for complicated calculation problems, using C to do the jobs and returning back the result into R for simpler jobs. Since I am new to C, I think it is easier for me to use R as a platform and use C for the complicated calculation rather than rewrite the whole code in C. However, before calling C code from R, I need to learn the basic knowledge about C so that I can create C function code.

While I am reading a C book, I found out several differences in C compared to R.

  1. C requires data type: ex) int, double, char, void and so on
  2. C has "main": kind of a function but special (every program need to have main)
  3. C has arrays rather than matrix: 1 dimensional array in C is treated as vector in R, 2 dimensional array in C treated as matrix in R, and 3 dimensional array in C is treated as list in R
  4. C has pointers: pointer holds a variable's address
  5. C has structures: kind of like an object where variables are grouped together


I am reading a book called "The C programming language" by Brian W. Kernichan and Dennis M. Ritchie. For more information, please read the book.