Recent Posts

All About Pointers in C - examples and interview questions

All About Pointers in C - examples and interview questions

All About Pointers in C - examples and interview questions


Pointers in C and C++, are the outstanding concept that helps to write more compact and efficient codes, due to which it very common in all coding interviews. So, today we will be covering all about the pointers that you need to know with examples, moreover we are listing a few questions that are generally asked into the interviews.


In C, a pointer is a variable that stores a memory address. It can be defined as a pointer in c is a variable that holds the address of another variable. Like all other variables, it also has a name that needs to be declared, and it also occupies space in the memory. A pointer makes a C program (code) more efficient and compact. Pointers can be initialized at the time of declaration, but, the variable whose address is to be stored inside the pointer, should be declared before the pointer. Remember pointers contain garbage value while declaration, i.e. it may be pointing anywhere in the memory. The general syntax of declaration of a pointer in c is-
Data_type *variable_name;
Example: Let’s assume we have to declare a pointer of integer type (int type in c), named alpha, then the syntax would be:
int *alpha;
Here, alpha is a pointer that should point to the variable of type int. And the type of alpha is pointer to int or (int *) or we can say that the base type of alpha is int.

It should be a concern that all the pointers will occupy the same space in the memory since all of them contain addresses only. Also, the size of a pointer depends on the architecture and may vary on the different machines.

Some commonly asked interview questions regarding Pointers in C:

  • List some uses of a pointer.
  • Ans: A pointer can be used in the following cases-

    • in accessing the array elements.
    • in returning more than 1 value from a function.
    • in accessing dynamically allocated memory.
    • in implementing various Data Structures like LinkedList, Trees, Graphs, etc.
  • Why pointers are called pointers?
  • Ans: They are called Pointers because they point to a particular location in the memory by storing the address of the location.

  • Why do pointers contain garbage value while declaration?
  • Ans: Pointers contain garbage value while declaration because at that time they may be pointing to any location in the memory.

  • What is the indirection operator (*) in C?
  • Ans: In C, an indirection operator provides access to a variable indirectly using a pointer. It is different from the asterisk (*) that was used while declaring the pointer variable. The indirection operator can be read as value at the address.E.g.:
    int a = 17; // declaring the variable
    int *p = &a; // declaring the pointer
    *p = 20; // this will assign a with 20, i.e. a = 20
    (*p)++; // this is equivalent to a++, i.e. 21

  • Differentiate between a pointer and a reference.
  • Ans:

    Pointer Reference
    Stores address of another variable. It is an alias for an already existing variable. Like a pointer, it can also store the address of an object.
    Declared with * sign. Declared with & sign.
    Can be re-assigned. Can’t be re-assigned.
    The value may or may not be assigned while initialization. The value must be assigned while initialization.
    A NULL value can be assigned. Can not assign any NULL value.
    Supports an extra level of indirection (i.e. has a pointer to pointer type of concept). Offers only one level of indirection (i.e. it has no such concept as reference to reference).
    E.g.: int v = 5; int *ptr = &v; E.g.: int v = 10; int &ref = v;


This was all about pointers in c, we'll discuss about Pointer to Pointer in the further posts.
Keep learning, keep practicing.
Until then, Happy Coding.

All About Pointers in C - examples and interview questions All About Pointers in C - examples and interview questions Reviewed by ATC Tech Adda on April 09, 2022 Rating: 5

No comments:

Powered by Blogger.