Saturday, October 3, 2009

Understanding Pointers

Ok, so before getting to multi-dimensional arrays and pointers let us understand what and how a pointer works?


To declare a pointer
*
int *px;


Now px is an interger pointer. To make this point to some variable.


int x;
px = &x;


px will now point to the address of x (& is called the reference operator or address operator).


To get the value from that address use * (called the dereference operator)


*px = 10;   /* equal to x = 10; */


Now for a single dimensional arrays.


int a[10];
int *pa;


pa = &a;


Now a[0] = *pa, a[1] = *(pa+1) and so on.


pa+1 means advance pointer to next location by 1 times the size of the pointer type.


So the above statement pa+1 means advance by pa by size of int, so now it points to a[1] after a[0].


Try a simple exercise now to do this. 


1. Use only pointers to get the sum of all elements in an array
2. Binary search using pointers.


Once we are clear with the pointer concepts


~Maddy

Friday, October 2, 2009

First Question.

First of all, congratulations to you on creating this blog.

Okay, here it goes

We can refer to array elements through pointers like this

int arr [8], *point ;
point = arr ;

Suppose I want to refer the third element

arr [2] is the same as *(point + 4)

How I do the same for multi-dimensional arrays ?

Books to Learn C

"The C Programming Language" by Kernighan & Ritchie is considered the Bhagawat Gita of C. I would definitely recommend this.


Programming in C by Stephen Kochan is also a wonderful book.


Programming Language: Concepts and Constructs by Ravi Sethi gives the complete picture on different data structures and its internal working.


Have a different book to share, suggest it.


Thanks
~Maddy

Indroduction

Hello,

Everyone knows that "C" is one the most widely used programming knowledges and is one of the oldest and best as well.

Few understand C immediately while some find it rather difficult. I am one of them who found C easy as well as tough. So if you are one among them finding it difficult then lets try to ask and solve questions, write programs for different Algorithms we know, problems we have and try to get to know C deeper via what is called easily the most difficult concept called "Pointers".

Lets start with basic questions you'll have on C like structure of a program, compiling, executing and basics like variables and operators and we will dive deep into Pointers where we will learn the real "C".

Thanks
~Maddy