Home > C Programming > Understanding 'C' Pointers

Understanding 'C' Pointers

October 15th, 2007 admin Leave a comment Go to comments

I write this post to help you understand what pointers are and how they are used.

I remember from my early college days when a ‘C’ pointer used to be a nightmare and I believe for many, it still is. I hope that this guide will help you understand Pointers in ‘C’ more clearly.




Before we begin our journey towards understanding what a pointer is and how to use it, lets understand how variables are stored in memory.

How variables are stored in memory?

Each byte of memory has a unique address. A variable’s address is the address of the first byte allocated to that variable. Suppose the following variables are declared in a program:

char alpha;
int number;
float amount;

The following diagram illustrates how these variables might be arranged in memory along with their addresses.

pointer1.gif

What is a Pointer?

A pointer is a special variable that holds the memory location of an other variable.

In earlier days of computing a pointer would have 2 bytes of space allocated. With rapidly increasing computing capacity and memory a pointer variable could have as much as 4 to 8 bytes allocated.

So what is a pointer… look at the definition mentioned above. Lets do this one step at a time in the three step process below:

1. Special Variable

“A pointer is a special variable that holds the memory location of an other variable.”

So a pointer is nothing but a variable… its a special variable. Why is it special, because… read point 2

2. Holds memory location

“A pointer is a special variable that holds the memory location of an other variable.”

It holds the memory location of another variable. By memory location I mean that it does not contain value of another variable, but it stores the memory address number (so to speak) of another variable. What is this other variable, read point 3.

3. Other variable

“A pointer is a special variable that holds the memory location of an other variable.”

Another variable could be anything… it could be a float, int, char, double, etc. As long as its a variable, its memory location on which it is created can be assigned to a pointer variable.

Diagrammatic Example

pointer-diagram1.gif

In the above example variable ‘a’ is an int variable with value ’5′. Variable ‘a’ has been allocated memory location 100 and 101 (remember that an int variable occupies 2 bytes – as per ANSI C)

Pointer ‘ptr‘ points to the variable ‘a’ i.e. has the memory location of variable ‘a’. Therefore you can reach to the memory location 100 using either variable name ‘a’ or pointer variable name ‘ptr’.




Using Pointers in ‘C’

Pointers in ‘C’ is implemented using the following operators:

& – called the address of operator – gets the addres of a variable
* – called the dereferencing operator – reaches to the memory address stored in pointer

Lets look at how to declare and use pointers in ‘C’:

1. #include <stdio.h>
2.
3. main() {
4.
5.
6. int count = 5;
7. int *ptr; //declaring a pointer
8.
9. ptr = &count; //assigning the memory of variable count to pointer ptr
10. printf(“Value of variable count is : $%d\n”, count);
11. printf(“Value of variable count using pointer is : %d\n”, *ptr);
12.
13 }

Explanation of the above program

  1. In the above program on line #6 we declare an integer variable ‘count’
  2. On line #7 we declare a pointer variable by the name ‘*ptr’. Please note that the * used here to declare the variable is not the dereferencing variable, but is only used to differentiate between a variable and a pointer.
  3. On line #9, we assign the address of variable count to the pointer ptr.
  4. On line #11 we use the pointer variable to reach to the memory location of the address stored in pointer ptr. Thus the dereferencing pointer is used to reach the original memory location stored in the pointer.

Please leave comments if you need more explanations or examples of C Pointers.

Categories: C Programming Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Dimityr
    November 16th, 2007 at 17:51 | #1

    Good explanation!

  2. James
    October 13th, 2009 at 08:58 | #2

    Great explanation. way better than the slides form my university, which just made things more complicated.

  3. Wang Wei
  4. me
    February 28th, 2010 at 07:01 | #4

    Good job! but,

    you forgot to explain why would we use INT double or whatever if you just point to the start of a place. Also if you declare it like that (int *ptr) why would you do mallocs with that pointer. Isn’t there an ambiguity?

    Can you explain these matters as clearly as you’ve done for the rest?

    Cheers

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )