C Pointers

  • Pointer is a variable which holds address of another variable.
  • We know that every variable is stored into memory locations. Each memory location have it’s address
  • The pointer operator * is called as value at address operator which gives value stored at a particular address.
  • The another operator use in pointer is & address of operator which gives address of a variable.
  • The ‘value at address’ operator is also called indirection operator.
  • A dereference operator (*) operator return the value of a variable pointed by a pointer.
  • Pointer variable must be declare before using it.
  • A type of a variable and a type of pointer should be same.

Pointers Notation

Consider the declaration,

This declaration tells the C compiler to:

  1. Reserve space in memory to hold the integer value.
  2. Associate the name a with this memory location.
  3. Store the value 10 at this location.

We may represent this declaration in memory as following-

pointers example

Here from above figure, we can see that value 10 is stored in memory location 12764. But it is not necessary every time value 10 will store at the same memory location. And this address is a number.
Hence we can print address of variable as shown in following program

Output:

Consider the following example, which shows how pointers work. From below figure, the type of a variable a (int*) is same as a type of pointer b (int *). Here b is a pointer which holds address of a variable a.

pointers

Output:

Now see an example of character data type:

Output:

Points to be noted:-


  • Here ptr and ch are declared as pointer variables that hold address.
    Addresses are always whole numbers. Therefore pointers always contain whole numbers.

  • This declaration does not mean that ptr contain a floating-point value.
    It means, ptr contain address of a floating-point value.
  • We know that pointer is a variable that holds an address of another variable.But this another variable can be a pointer. So we can say that pointer can hold an address of another pointer.
    Likewise, there could exist a pointer to a pointer to a pointer to a pointer to a pointer.
  • There is no limit on how far can we go on extending this definition.
Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.