Categories: Programming & DS

C Book Chapter 3: Pointers

Pointer as the word implies just points. It is a datatype in C which is used to hold memory address. Pointers may point to any datatype in C and based on the data type pointer arithmetic is done.

* Operator

A pointer is declared using * as follows:

int* p; 
/*p is a pointer to int. int *p is also syntactically correct
but * here is always taken with int as int* and p is the name
for the variable. Many take this as *p which is not correct */

char* y; //y is a pointer to char

Actually pointer operation is very simple as there are just one operator for using pointer which is the dereferencing operator *.

*p -> gives the content of the location in p.

What the content is, depends on the type of p. If p is an integer pointer, *p will return 4 bytes from the location contained in p. If p is a char pointer *p will return 1 byte from the location in p.

Example

int *p, a;
scanf("%d",&a); /*memory address of a is passed to scanf 
and it stores the keyboard entered value in that location */
p = &a; //Memory address of a is stored in p printf("*p = %d", *p); /*Content of p is printed by printf.
Since %d is used, 4 bytes (assuming sizeof int is 4) from
the memory location given by p is converted to integer and printed.*/

 

The below code also does the same job but in a slightly different manner.

int *p, a;
p = &a;
scanf("%d",p);
printf("a = %d *p = %d", a, *p);

Pointer Arithmetic

Why do we do pointer arithmetic? It is to access the next or previous elements in an array of elements. For that reason pointer arithmetic is restricted to just addition and subtraction. And though pointers can be represented using integer values, as they hold memory addresses, pointer arithmetic is different from integer arithmetic. Pointer arithmetic works as follows:

//Assume p and q are pointers
 
p+1 /*p is incremented to the next address for holding the 
data type for p. i.e; if p is an int pointer p+2 will add 8
to the content of p (assuming sizeof (int) is 4) */ p-1 /*p is decremented to the previous address for
holding the data type of p */ p-q /*If p and q are pointers p-q will work as above and
thus will return the no. of objects of type of p between
the memory addressess of p and q (p and q must be of same
data type or else it is compilation error) */ p+q //Not allowed and throws compilation error

 There are no multiplication or division operators in pointer arithmetic as they have no meaning. 

Does a pointer have memory?

Yes, pointer is a data type and so has a memory for it. On a 32 bit system, pointer requires 4 bytes (32 bits) of memory while on a 64 bit system it requires 8 bytes. And it is the same for pointer to any data type. Since pointer has a memory, we can have a pointer to pointer as well and this can be extended to any level.

Example

 #include <stdio.h>
 int main() 
 {
   int a; 
   int* p;
   p = &a;
   int** q; //q is a pointer to a pointer to int
   q = &p;
   printf("a = %d, *p =%d, **q = %d\n", a, *p, **q);
 }

 

Assigning values to pointers

Since pointers should hold valid memory address (OS allocates certain memory region for a process and it should use only that region and should not try to access other memory region), we should not assign random values to a pointer variable. Assigning some values to a pointer is allowed, but if that value is not in the memory address for the process, dereferencing that pointer using * operator will cause segmentation fault.

One way of assigning value to a pointer variable is by using & operator on an already defined variable. Thus, the pointer variable will now be holding the address of that variable. The other way is to use malloc function which returns an array of dynamic memory created on the heap. This method is usually used to create dynamic arrays.

Example

#include <stdio.h>
#include <stdlib.h> //contains the declaration for malloc
int main() 
{
  int a; 
  int* p = &a;
  *p = 6;
  p = malloc(10 * sizeof(int));
  *(p+5) = 7;
  printf("a = %d, *(p+5) = %d, p[5] = %d\n", a, *(p+5), p[5]);
}

 

Problems with pointers

Pointers are very powerful but with power comes problems. Since, pointer allows direct access to memory address, if programmer is not careful, pointer usage can cause segmentation faults. The other problem with pointer is that pointer dereferencing is difficult to debug especially when there are more than two levels of indirection (pointer to pointers).

Exercise Questions

    arjun

    View Comments

    Share
    Published by
    arjun

    Recent Posts

    GATE CSE 2022 Admissions, Results and Placement Responses

    This page shows all details regarding GATE CSE 2022 Admissions including results, admission offers and…

    2 years ago

    GATE CSE 2021 Admission Responses

    Source Add your Response Rank Predictor for GATE CSE 2021 Result Responses: GATE CSE 2021…

    3 years ago

    GATE CSE Books – More Options

    Best Books for GATE CSE with Relevant Chapters to Read  Heads Up! These GATE CSE…

    3 years ago

    ISI PCB Previous Year Papers with Solution

    Indian Statistical Institute(ISI) offers M Tech in Computer Science with the Admission Test Codes MMA/PCA…

    3 years ago

    ISI JRF Previous Year Papers with Solution

    Indian Statistical Institute(ISI) offers Junior Research Fellowships (JRF) in Computer Science, Statistics, Mathematics, Quantitative Economics,…

    3 years ago

    ISI DCG Previous Year Papers with Solution

     Indian Statistical Institute(ISI) conduct the admission test for Postgraduate Diploma in Computer Applications(PGDCA) with the…

    3 years ago