How2Lab Logo
tech guide & how tos..


Pointers in C - Part 1 of 9


Consider a variable v of some data type. During program execution, space is allocated in the computer's memory to store the data value that v represents. The amount of memory space allocated will depend on the data type of the variable v. For instance, if data type of v is char, a space of 1 byte will be allocated, whereas if v is an integer or a floating point number, a space of 4 contiguous bytes will be allocated in the memory.

The address of the memory space allocated to v is denoted by an expression &v. This is also referred as the left value of v (l- value). The actual data value stored in v is referred as the right value of v (r-value).

A variable p is called a pointer to v, if it points to the location where v is stored, i.e. if it stores the l-value of v.

The syntax for declaring a pointer variable is:

<data type> *<identifier>;

Usage of pointer can be understood by the code snippet below:

int v; 
int *p; //declaration of pointer to an integer
p = &v; //assigns the address v to p

With the above assignment, p is now a pointer to v.


Since a pointer variable points to a memory location, the content of that location can be obtained by prefixing the pointer variable by the unary operator*, which is also referred as the indirection or dereferencing operator. Here is the syntax for this:*<pointer_variable>.

Example

int u, v = 9;
int *p; 
p = &v; //p points to the location of v
u = *p;

In the above example, *p assigns the value 9 to u, viz. the content of the location pointed to by p, i.e. the r-value of v.


Points to note:

  • The unary operators & and * have same precedence as the other unary operators-, --, +, ++, !, sizeof and (type).
  • A pointer may be assigned (i.e. initialized) a null value, when it does not point to any data item.
  • The address operator & can be applied only to an operand which has a unique address e.g. to variables or single array elements. It cannot be applied to arithmetic expressions.
  • The indirection operator * can be applied to operands that are pointer variables. When a pointer p points to another variable v with the assignment p = &v, then in an expression, *p and v can be used interchangeably.

Here is another example to illustrate usage of the indirection operator:

int v = 9;
int *p; 
p = &v;
*p = 0; //resets v indirectly

Operations on Pointers

A pointer can be made to point different locations in the memory by performing operations such as adding or subtracting integer quantity to or from a pointer variable. The pointer shifts (due to such operation) to a new location. The new location is determined by adding or subtracting (depending on the operator used in the operation), the product of the offset (integer quantity indicating the extent of shift) and the scaling factor associated with the data type of that pointer. To understand what we mean by this, look at the following example:

#include <stdio.h> 
main()
{
	static int j[5] = {1, 3, 5, 7, 9}; 
	int *p; //defines a pointer to an integer

	p = j;
	printf("%d", *p); //will print 1

	p = p + 3;
	printf("%d", *p); //will print 7
	...

In the above code snippet, the statement p = j assigns the r-value of j to p. Now, we know that the array name stores the address of the first element of the array, viz. &j[0] in this case. Thus the assignment p = j essentially is same as p = &j[0]. At this point *p will print the value of the first element of array j, which is 1.

The statement p = p + 3; will increment the pointer p so that the pointer p would point to a new location which can be computed using the following calculation:

New position of p = Old position of p + (offset x scaling factor)

Since p is a pointer to an integer, and we know that int typically occupies 4 bytes in memory; the scaling factor becomes 4. The offset added to p in this operation is 3. So by substituting the values in places we finally get:

p = p + (3 x 4);

This means that p is incremented by 12 bytes, making it now point to the new location address &j[3] which is address of the element 7.


More about Pointer Arithmetic

We will now illustrate the usage of increment operator (++), decrement operator (--), and the indirection operator (*) in implementing pointer arithmetic. The ++, -- and * operators fall in the same precedence group and their associativity is right to left.

Consider the following array declaration for evaluating the results of the expressions that involve pointer arithmetic:

int arr[4] = {1,2,3,4};
int *p;
p = arr;  //p points to the first element in the array

Now look at the following cases. All the expressions listed below are assumed to be the statement immediately following the last statement in the above code snippet, i.e. the statement p = arr;.

ExpressionEvaluation
++pThe pointer p points to the second element in the array
--pThe pointer p points back to the element 1 again
++ *pSince the unary operators are associated right to left, the expression *p is evaluated first and then ++ operator is applied. So the value of the element that p is pointing to will be accessed first (due to *p) and then the value is incremented by 1 (due to ++). Since p was pointing the first element, i.e. arr[0], its r-value is incremented and therefore the expression ++ *p evaluates to 2.
(*p)++This is the same as previous one.
*++pIn this case the ++ operator will be applied on p first, followed by the indirection operator *. Thus, first the pointer p will jump to the next element of arr, which is arr[1], viz. the element whose value is 2. It accesses the content of the second element of arr and therefore the expression *++p will evaluate to 2.
*(p++)Same as previous
*p++Same as previous

Note: The following operations on pointers are not allowed.

  1. Addition of two pointers
  2. Multiplying a pointer with a number
  3. Dividing a pointer with a number

Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.