14.1 – Introduction to pointers

by subbu on January 14, 2014

In this chapter we are going to explore the most important and exciting concept of C language, pointers. Most people feel that, it is the difficult part of C language but, believe me it is the simplest and interesting part of C language. Only you need to do is follow and practice every page of this chapter with bit patience. Explanatory pictures and ready to execute programs are given to make pointers simple.

Advantages of pointers

  • Some times pointers are only the way of doing certain things, can’t be done otherwise
  • We can develop more compact and efficient code using pointers.
  • Impossible things can be done by using pointers with care.
  • Good programmers always use pointers to write compact and efficient programs.

Let us start out journey with memory.

Computer memory:

RAM is the primary or main memory of computer. It is the place where the text, data, instructions and intermediate results are stored. The total memory is divided into number of bytes and each byte is again divided into 8 bits. These bits are actual places where the data is stored as 1’s and 0’s called binary data. Every byte is identified with a number called address. It is always a positive number because the first byte address is 0 and the last byte address is depends on the size of memory.

computer memory

Variable

A variable is a name, set to a piece of memory in the RAM, which can be used to store and access the data. The value stored in the allocated (selected) memory may change as the program execution progresses so called variable.

A variable may take one or more bytes of memory according to the data type we use while declaring the variable.

Any variable of any type has three common characteristics, which are

  1. Name of the variable,
  2. Value of the variable and
  3. The Address of variable

Characteristics of a variable

Name is the label of a variable (memory allocation) with which value can be assigned, modified and accessed

price=45.75;            /* assigning a value    */
price=price+10.50;   /* modifying the value */
printf("%f",price);     /* accessing and printing the value */

Value is the data of any type stored in the memory in a binary format.

Address

As we discoursed earlier, the total memory is divided into number of bytes and each byte in the memory is identified with its serial number or byte number or we can call it as an address. It is always a positive number.

According to the type, a variable may take one or more bytes of memory but, the first byte number or higher byte number is considered as the address of variable.

Address of a variable

How to get the address of a variable?

We can get the address of any variable using a unary operator called Address of operator &. Writing & operator as a prefix to the variable gives the address of variable. As the address of any variable is a positive number, the format specifier %u or %p or %x is used while printing the address. %u prints the address in decimal, %p and %x print the addresses in hexadecimal.

#include <stdio.h>
int main()
{
short x=10;
printf("Value %d",x);                 /* prints the value of x   */
printf("\nAddress of x %u",&x);  /* prints the address of x in decimal */
printf("\nAddress of x %x",&x);  /* prints the address of x in hexadecimal */
printf("\nAddress of x %p",&x);  /* prints the address of x in hexadecimal */
return 0;
}

Output in Turbo C:

Value 10
Address of x 65524
Address of x fff4
Address of x FFF4P

Address of operator

As the turbo C 3.0 is developed for 16bit system, address of any variable is a 16bit positive number (65524 –  1111 1111 1111 0100)

Output in Linux gcc:

Execution1:
x=10
Address of x 3217705836
Address of x bfca4b6c
Address of x oxbfca4b6c

Execution2:
x=10
Address of x 3220131100
Address of x 0xbfef4d1c

As the Ubuntu Linux 9.10 gcc is developed for 32bit systems, address of any variable is a 32bit number (3220131100 – 1011 1111 1110 1111 0100 1101 0001 1100).

Note: The address of a variable changes execution to execution that we can observe from the above two executions.

#include<stdio.h>
int main()
{
short x=45,y=52;
printf("x=%d",x);
printf("\ny=%d",y);
printf("\nAddress of x %u",&x);
printf("\nAddress of y %u",&y);
return 0;
}

Output:
x=45
y=52
Address of x 65494
Address of y 65492

Addresses of two variables

Here x and y are a series of variables of short type, the memory allocation of which allocate in adjacent memory allocations.

We can’t get the address of a register variable

The storage specifier “register” allocates the variable in the CPU registers. We know that, it is not possible to get the address of a CPU register. Hence the compiler throws an error when we try to access the address of a register variable.

#include<stdio.h>
int main()
{
register short x=1;
printf("\nValue of x %d",x);
printf("\nAddress of x %u",&x);
return 0;
}

Output:
Error demo.c 6: Must take address of a memory location in function main
*** 1 errors in Compile ***

Summarizing address of operator &

  • When a variable is declared then the memory of which is allocated in the free memory.
  • Any variable of any type may take one or more bytes of memory but the higher byte number is considered as the address of memory allocation. It is always a positive number (unsigned). %u ,%x or %p are used to print the address.
  • Address of (&) is a unary operator used to get the address of any specified variable.
  • We can’t use the address of operator with the register variable.

Previous post:

Next post: