Pointers in C for GATE Students

The Basics of Pointers : What is a Pointer ?

A pointer is a variable that holds the memory address, usually the location of another variable in the memory.

Declaration and Initialization of a Pointer:

General From of Declaration of pointer :
type * var_name; 

where type is any valid C++ data type and
      var_name is the name of the pointer variable.
Example of declaring a Pointer :
int *iptr;        //creates an integer pointer iptr
char *cptr;       // creates a character pointer cptr
float *fptr;      // creates a float pointer fptr
Explanation of above Declaration of a pointer :
  1. iptr is an integer pointer, means that the memory location being pointed to by iptr can hold only integer values. And in other words, it can be stated as iptr is a pointer to integer.
  2. cptr is a character pointer, means that the memory location being pointed to by cptr can hold only character values. And in other words, it can be stated as cptr is a pointer to character.
  3. fptr is a float pointer, means that the memory location being pointed to by cptr can hold only float values. And in other words, it can be stated as fptr is a pointer to a floating point value.

In short, it is the base type of the pointer that defines what types of variables the pointer can point to or what type of variables the pointer will store.

Initialization of a pointer :

Before Initialization of a pointer, Let us understand about two operators :

* and &

* → Deferencing Operator
& → Referencing Operator

* → The * is a unary operator which returns the value of the variable located at the address following it (*-)
& → It is also a unary operator, but does the reverse of *. The unary operator & returns the memory address of its operand.

* → Value at Address
& → Address
Example of Initialization :
int i = 25;       //declares an int variable i
int *iptr;        //declares an int pointer iptr
iptr = &i;        //stores the memory address of i into iptr
Explanation of above Initialization :

Pointers in C for GATE Students 1

Some More Basics on Pointer, So that GATE Students can solve the programs coming in GATE Exam

Point Number 1 :
What will be the output of sizeof() function during initialization of variable of different data type ?

Let the processor is of 16 bit, then size of address is 2.

Statement : What will be the output when sizeof() function will be used ?
int a = 10; sizeof(a) = 2 Byte sizeof(&a) = 2 Byte
float b = 2.54 sizeof(b) = 4 Byte sizeof(&b) = 2 Byte
char c =’a’ sizeof(c) = 1 Byte sizeof(&c) = 2 Byte

If the  the processor is of 32 bit, then size of address is 4 and if the processor is of 64 bit, then size of address is 8.
So, The value of sizeof(&a), sizeof(&b) and sizeof(&c) will be compiler dependent. The compiler dependent problems will never come in GATE exam. The size of the address is always given in the exam before asking the question.

Point Number 2 :
What will be the Meaning/Output of the below statements ?
  Statement Meaning
int a = 10; Pointers in C for GATE Students 258
int *b = a; Pointers in C for GATE Students 9
  Statement Output
printf(“%d”,a)  10
printf(“%u”,b)  10
printf(“%d”,*b) Pointers in C for GATE Students 10
  Statement Meaning
int a = 10; Pointers in C for GATE Students 258
int *b = &a; Pointers in C for GATE Students 6
  Statement Output :
printf(“%d”,a)  10
printf(“%d”,*b) Pointers in C for GATE Students 7
printf(“%u”,b)  1000

Some Programs Regarding the basics – Click for the Programs

     

Incoming search terms:

  • c for gate
  • c programs asked in gate exam

Leave a Reply