Friday, May 16, 2014

Pointers-1

  I thought of starting this blog to share my experiences with the C programming language. Briefly speaking about myself, I have done my masters in Physics and very briefly I have worked in Computational Astrophysics. Presently I work at a well-known company in the field of engineering and scientific computing. I work in the HPC team of one of their products.


  I did not have any proper introduction to the C programming language, like most of us; moreover, I always tried learning by doing instead of reading from somewhere, which I find extremely painful and boring. Clearly this is not the right way, but I chose to follow it. I faced quite a few difficulties and thought to write about them for people who don't have problem reading! :P


  I want to talk about pointers in my first post. So, pointers are a special kind of variable (like float, int, double etc.) to store a number representing address of a memory location (RAM). So, if you have 2 Giga-Bytes (2 * 1024 * 1024 * 1024 = 2147483648 Bytes) of memory in your system, each bytes are numbered maybe from 0 to that number. A pointer variable stores that number, just like an int variable stores an integer. Since, memory locations start from 0, pointers are much like unsigned integer.


  You tell the compiler a memory location, mention the type of variable it should interpret when it reads from that location, and it gives you the value of a variable the pointer is supposed to be pointing to. Let us look at the following example-