Notes on Pointers in C programming | Grade 12 > Computer Science > Programming in C | KULLABS.COM

Pointers in C programming

  • Note
  • Things to remember

The memory of the computer is organized as a sequence of bite-sized locations (1 byte =8 bits).These bytes or memory locations are numbered beginning with zero. The number associated with a byte is known as its address or memory location.

A pointer is an entity which contains the memory address. Thus, a pointer is a number which specifies a location in the memory of the computer. C uses byte as the basic unit of memory.

Each byte is numbered by an address for its reference. Generally, 1 byte is used for char, 2 bytes for the int, 4 bytes for float and 8 bytes for double. It is possible to directly access the addresses in C by using pointers.

The fundamental concept of pointers is:

  1. Each byte in memory is associated with a unique address.
  2. An address is an integer having fixed number of bits.
  3. Addresses are positive integer values that range from zero (0) to last location of the memory (65535 in a case of 64k).
  4. Every object (data or program code) located in memory is associated with a memory address.
  5. Each variable and each function in the program start at a particular location and spans across consecutive addresses from that point onwards depending upon the size of the data item.
  6. The size of data type referenced by the pointer is the number of bytes that may be accessed directly using that address.

Hence, programs can be written efficiently and compactly with the help of pointers. Careless use of pointers causes unexpected errors or difficulties in the execution of programs.

Relative Addressing and Absolute Addressing

The two common types of addressing are:

  1. Relative addressing and
  2. Absolute addressing.

Most of the computers use the convention of relative addressing mode to access memory. In this case, the address consists of two components, the base (or segment) address, and the offset address. The base address designates the specified segment of the memory and the offset specifies the distance of the desired memory location from the beginning of that segment

.

The use of relative addressing simplifies the management of large memory.

All relative addresses are first translated into corresponding absolute addresses prior to physically accessing the memory. Corresponding to given physical address or absolute address, it is possible to have the different relative address.

.

Hence, a pointer is a valid address, which is stored in a pointer variable. A pointer variable is declared like an ordinary variable, with * proceeding in a pointer variable name.

The Address Operator ‘&’

We know that all the variables defined in a program; including pointer variables reside at specified address of the computer’s memory. It is possible to obtain the address of a program variable by using address operator & (ampersand).

This is used as the prefix with the variable name and it gives the address of that variable. The address operator & and the indirection operator * are exclusively used in pointers. They precede their operand. The operator & requires 1 value like a variable as its operand and it returns the address of its operand.

The indirection operator * requires a pointer expression as its operand and it returns the data object stored in the operand. The execution of the declaration statement results in allocating storage spaces only and the data objects are not assigned automatically. After the declaration of the pointer variables, they must be initialized.

Uses of Pointers

Dynamic memory allocation is a programming concept wherein the use of pointers becomes indispensable. Variables can be created and defined when the program is being executed. Memory can be obtained from the system and allocates for the creation of the new variable or a data structure.This is called dynamic memory allocation.

Dynamic variables created in this fashion can only be accessed by the use of pointers. Pointers offer tremendous flexibility in the creation of dynamic variables, accessing and manipulating the contents of memory locations, and releasing the memory occupied by the dynamic variables which are no longer needed.

The major uses of pointers are:

  • Dynamic memory management.
  • Accessing array elements.
  • Passing arguments to functions by reference.
  • Passing arguments arrays and strings to functions.
  • Creating data structures such as linked lists, trees, graphs, etc.
  • Obtaining memory from the system dynamically.

Pointer Variable

A pointer variable is defined as the variable that contains the memory address of data or executable code. A pointer variable can be declared so that it works only with data of given type. As an integer variable can hold only integers, a character variable can hold only the characters, similarly, the pointer variable can point only to one specific type (int, char, float or double or any user-defined data type) of data.

Pointer variables can point to numeric or character variables, arrays, functions or other pointer variables. A pointer variable can be assigned the address of an ordinary variable. A pointer variable can be assigned the value of another pointer variable provided both pointer variables and print to data items of the same type. A pointer variable can be assigned a null (zero) value.

Initializing Pointer Variable

A pointer variable can be initialized by using static or dynamic memory allocation. In static memory allocation, the space reserved by the compiler is assigned to a pointer variable. Dynamic memory allocation is obtained by using the built-in functions like malloc() and calloc(). The dynamically created memory may be freed using the function free().

A pointer variable can be declared as:

<data type> *<ptvar_name>;

The <data type> indicates the type of data such as int, char, float, double or user defined structures. The ptvar_name could be any valid C variable name. The presence of the asterisk (*) preceding the <ptcvar_name> indicates the compiler that it is a pointer variable. The pointer declared in this way can hold the address of any variable of the specified data type.

Examples:

int *intptr;

char *chptr;

Length *1ptr; /* pointer to user-defined data type such as Length */

Float a, b;

Float *pv;

Here, float a, b indicates that two variables are declared in the C program. But, float *pv indicates the pointer variable named pv is declared. It is float type of variable and it can point to only float type of data.

int a=10;

int *p;

P=&a;

Here, *p is the declaration of pointer variable and p=&a states that & address operator returns address of an integer value a. After assigning the address of a location of a variable to a pointer variable, the value of that variable can be accessed by using special indirection operator called unary operator (*asterisk).

int *p ;variable to point address of int value

a float *p; variable to point address of float point value

char *p; variable to point address of char variable.

# Write a program to find the address and actual value located in the memory locations.

#include <stdio.h>

#include <conio.h>

void main()

{

clrscr ();

int a;

int *p;

a =50;

p =&a;

printf(“\n Address of a =%u”, &a);

printf(“\n Address of a =%u”, a);

printf(“\n Address of p =%u”, &p);

printf(“\n Address of a =%d”, a);

printf(“\n Address of p =%d”, *a);

}

# Write a program to demonstrate pointers and addresses of the values.

#include <stdio.h>

#include <conio.h>

void main ()

{

clrscr ();

int a, *p;

a =50;

p = &a;

printf(“\n Before changing the value of a = %u”, *p);

printf(“\n The address at this is %u”, *p);

*p-=5;

printf(“\n after changing the value of a = %u”, *p);

printf(“\n The address at this is %u”, &p);

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 250-254.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd



  1. A pointer is an entity which contains a memory address. Thus, a pointer is a number which specifies a location in the memory of the computer. C uses byte as the basic unit of memory.
  2.  Each byte is numbered by an address for its reference. Generally, 1 byte is used for char, 2 bytes for an int, 4 bytes for float and 8 bytes for double. It is possible to directly access the addresses in C by using pointers.
  3. Memory can be obtained from the system and allocates for the creation of the new variable or a data structure.This is called dynamic memory allocation
  4. A pointer variable is defined as a variable that contains the memory address of data or executable code. 
  5. A pointer variable can be assigned the value of another pointer variable provided both pointer variables and print to data items of the same type. A pointer variable can be assigned a null (zero) value.
  6. In static memory allocation, the space reserved by the compiler is assigned to a pointer variable. Dynamic memory allocation is obtained by using the built-in functions like malloc() and calloc(). The dynamically created memory may be freed using the function free().
.

Very Short Questions

0%

ASK ANY QUESTION ON Pointers in C programming

No discussion on this note yet. Be first to comment on this note