Pointer and its manifestations


int x=5;

int *ptr;

ptr = &x;

Let us think about where these value variables and the pointer variables are located in memory. Some times in our school days we may image that a location in memory is marked as x and a value 5 is tored in it. Yeh .. now we know that it the Symbol Table that tackles the issue. That is the compiler creates a data structure called symbol table and in it the variables/symbol names along with the addess of the memory location is maintained. For a pointer variable the size is usally 4 bytes. And here the memory location assigned for ptr will hold the address of memory location assigned for the value variable x.The pointer arithmetic is dependent on the data type of the pointer.

As we use the expression ptr = &x; … the address of value variable ‘x’ is extracted from symbol table and stored in ptr. Thus ptr holds the address of x, and we can access the content of ‘x’ using ptr. Eg: printf(“%d”,*x);

Pointer to an array

int x[10];

int *arr;

arr = x;

Yeh .. true because compiler considers array as pointer. What happens when we say like … arr++. The content of arr is incremented obviously and in this case it is incremented by the size of data type i.e 2 for 16 bit O.S.

Array of pointer is generally used to hold a bunch of addresses that has some logical relation.

Pointer to structure can be used to allocate a block that contains sub elements and the block can be released at will.

Eg:

struct Node{

int val;

char fName[24];

};

struct Node *n;

The pointer variable ‘n’ initally occupies only 4 bytes and the block corresponding to structure is allocated only when the programmer applies memory allocation function.

Eg: n = (struct Node*)malloc(sizeof(struct Node));

After use we can release at will by using free(n);

Thus this helps in dynamic memory management which yields better efficiency.

Pointer to a charecter

Pointer can be used to point to a charecter sequence. Eg: char *msg ;

msg = (char *)malloc(20);

strcpy(msg,”Hello World”);

In this case msg points to the first byte or the zeroth element of the block of memory. By using msg++, we can point to the next successive byte and so on.

*(msg)++ in this case would increment the content of the memory location that it is pointing to. For example if it is pointing to ‘e’ then the value is incremented to ‘f’.

character pointer to pointer  is usually y used for referring an array of string or something like a two dimensional character array.

Eg: char **words = new char[20][20];

( I have used C++ style of allocation to mention its resemblance to two dimensional character arrays)

Advertisements

One thought on “Pointer and its manifestations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s