Subject: Computer Science
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:
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.
The two common types of addressing are:
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.
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.
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:
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.
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.
Length *1ptr; /* pointer to user-defined data type such as Length */
Float a, b;
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.
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.
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.
void main ()
int a, *p;
p = &a;
printf(“\n Before changing the value of a = %u”, *p);
printf(“\n The address at this is %u”, *p);
printf(“\n after changing the value of a = %u”, *p);
printf(“\n The address at this is %u”, &p);
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
© 2019-20 Kullabs. All Rights Reserved.