Pointer Interview Questions in C/C++ with Answers (2024)

Pointer Interview Questions in C

This article is mainly focused on the most repeatedly asked and the latest updated Pointer Interview Questions in C that appear in most of the C/C++ interviews.

If you are looking for “Pointer Interview Questions in C/C++” or “advanced questions on pointers in C/C++, then you are at the right place. Here I have tried to create a collection of “pointer interview questions with answers in C/C++ ” that might ask by your interviewer.

I hope these C pointer interview questions with the answer will be helpful. If you want to update this pointer interview questions list or have any other important questions related to pointers in C/C++, please write in the comment box.  We will update this pointers questions list and we will give you credit. It will be helpful for others.

 

Q) What is Pointer in C?

Answer:

A pointer is similar to a variable but the difference is that pointers store the address of a location in memory and the variable stores the value. In other words, we can say, a pointer is used to reference a location in the memory.

When we have used a pointer to store the address in the memory than using the dereferencing techniques we can also get the value from the address which is stored by the pointer.

Syntax of a pointer in C:

The Declaration of a pointer is very important because at the time of declaration you define the capability of the pointer. Every pointer has the data types (pre-defined or user-defined) and names followed by an asterisk (*). Asterisk is a unary operator.

Data_Type * Pointer_Name;

Let’s see the below-mentioned example to understand the declaration of a pointer.

char *cPtr // pointer to a character
int *iPtr; // pointer to an integer
float *fPtr; // pointer to a float
double *dPtr; // pointer to a double

 

Q) What is a NULL pointer?

Answer:

According to the C standard, an integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer is called a null pointer.

int *piData = NULL;  // piData is a null pointer

 

Q) What is the wild pointer?

Answer:

A pointer that is not initialized properly before its first use is known as the wild pointer. The uninitialized pointer’s behavior is undefined because it may point to some arbitrary location that can be the cause of the program crash, that’s the reason it is called a wild pointer.

In other words, we can say every pointer in programming languages that are not initialized either by the compiler or programmer begins as a wild pointer.

Note: Generally, compilers warn about the wild pointer.

int *piData; //piData is wild pointer

 

Q) What are void or Generic pointers in C?

Answer:

A void pointer is a generic pointer, it has no associated data type. It can store the address of any type of object and it can be type-casted to any type. According to the C standard, the pointer to void shall have the same representation and alignment requirements as a pointer to a character type. A void pointer declaration is similar to the normal pointer, but the difference is that instead of data types we use the void keyword.

Syntax:

void * Pointer_Name;

 

Q) What is dangling pointer in C?

Answer:

Generally, daggling pointers arise when the referencing object is deleted or deallocated, without changing the value of the pointers. It creates a problem because the pointer is still pointing to the memory that is not available. When the user tries to dereference the daggling pointers then it shows the undefined behavior and can be the cause of the segmentation fault.

In simple words, we can say that a dangling pointer is a pointer that does not point to a valid object of the appropriate type and it can be the cause of the undefined behavior.

Let’s see the below image for a better understanding.

In the image Pointer1, Pointer2 is pointing to a valid memory object but Pointer3 is pointing to a memory object that has been already deallocated. So Pointer3 becomes a dangling pointer when you will try to access the Pointer3 then you will get the undefined result or segmentation fault.

dangling pointer

You can see the Articles for more detail,

 

Q) What is the usage of the pointer in C?

Answer:

There is a lot of usage of the pointers in C but here I am mentioning some important usage of the pointer in C that you must know.

  • Pointer is mostly used in dynamic memory allocation. Using the memory management function we can get the memory during the execution of a program.
  • Pointers can be used to access the array of elements.
  • We can access the memory location with the help of C Pointers.
  • Pointers are used in “call by reference“.  In which we can pass the address of a variable ( function, array, ..etc) into a function.
  • Pointers are used in complex data structures like linked lists, trees, etc.

 

Q) What is the difference between pass by value by reference in c and pass by reference in c?

Answer:

Pass By Value:

  • In this method, the value of the variable is passed. Changes made to formal will not affect the actual parameters.
  • Different memory locations will be created for both variables.
  • Here there will be a temporary variable created in the function stack that does not affect the original variable.

Pass By Reference :

  • In Pass by reference, an address of the variable is passed to a function.
  • Whatever changes are made to the formal parameter will affect the value of actual parameters(a variable whose address is passed).
  • Both formal and actual parameters shared the same memory location.
  • it is useful when you are required to return more than 1 value.

 

Q) Is that possible to add pointers to each other?

Answer:

No, it is not recommended to add two-pointers. See this article for more detail: Arithmetic operation on pointers.

 

Q) What is a far pointer in C?

Answer:

A far pointer is typically 32- bit pointer that can access memory outside the current segment. To use this, the compiler allocates a segment register to store the segment address, then another register to store offset within the current segment.

 

Q) What is a near pointer in C?

Answer:

A near pointer is a pointer that is used to bit address of up to 16 bits in a given section of the computer memory that is 16 bit enabled. The limitation is that we can only access 64 kb of data at a time.

 

Q) What is the difference between near, far and huge pointers?

Answer:

The differences are only relevant On 16-bit intel architectures. On a 16-bit x86 segmented memory architecture, four registers are used to refer to the respective segments:

DS → data segment
CS → code segment
SS → stack segment
ES → extra segment

A logical address of this architecture is composed of the segment and an offset. Now let’s see the difference between near-far and huge pointers.

  • The near pointers refer (as an offset) to the current segment. They don’t have a selector they have an implied selector. The near pointers can access 64k off the virtual address space.
  • The Far pointers use segment info and an offset to point across segments. They have an explicit selector. However, when you do pointer arithmetic on them the selector isn’t modified.
  • The huge pointers have an explicit selector. When you do pointer arithmetic on them though the selector can change.

Note: On modern platforms (32-bit and 64-bit architectures) memory models are using segments differently. And these keyword is not part of the C standard.

 

Q) What is the size of a void pointer in C?

Answer:

The size of a void pointer is similar to the size of the character pointer. According to the C standard, the pointer to void shall have the same representation and alignment requirements as a pointer to a character type.

 

Q) What is the difference between an uninitialized pointer and a null pointer?

Answer:

An uninitialized pointer is a pointer that points unknown memory location. The behavior of the uninitialized pointer is undefined. If you try to dereference the uninitialized pointer code behavior will undefine.

According to the C standard, an integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. The behavior of the uninitialized pointer is defined. When you try to dereference the null pointer then your code will crash.

 

Q) What is the usage of the NULL pointer in C?

Answer:

There is a lot of usage of the null pointers in C but here I am mentioning some important usage of the null pointer in C that you must know.

  • A pointer that is not pointing to the address of a valid object or valid memory should be initialized to NULL. It prevents the pointer to become a dangling pointer and ensures the programmer that the pointer is not pointing anywhere.
char *pcData = NULL; //Prevent to become dangling pointer

  • A very good habit to check the validity of the pointers before using them. It prevents the crashing of the code and unwanted results. The null pointer helps you in error handling.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *piData = NULL;
    piData = malloc(sizeof(int)*10);
    if(NULL == piData)
    {
        //exit
    }
    else
    {
        //code
        free(piData); //free after the use
    }
    return 0;
}
  • There are a lot of library functions in C where pointer arguments are optional. So passing the null pointer to a function argument is useful when you don’t want to pass any valid memory or object address.

For example,

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
  • In a summary, you can understand that a null pointer can be used as an error value, a sentinel value, or terminate indirection in the recursive data structure.

 

Q) What is the meaning of the below declarations?

1. const int a;
2. int const a;
3. const int *a;
4. int * const a;
5. int const * a const;

Answer:

  1. The “a” is a constant integer.
  2. Similar to the first, “a” is a constant integer.
  3. Here “a” is a pointer to a const integer, the value of the integer is not modifiable, but the pointer is not modifiable.
  4. Here “a” is a const pointer to an integer, the value of the pointed integer is modifiable, but the pointer is not modifiable.
  5. Here “a” is a const pointer to a const integer which means the value of the pointed integer and pointer both are not modifiable.

You can check the article “Clockwise/Spiral Rule in C/C++ with Examples“.

 

Q) Differentiate between a constant pointer and pointer to a constant?

Answer:

Constant pointer:

constant pointer is a pointer whose value (pointed address) is not modifiable. If you will try to modify the pointer value, you will get the compiler error.

A constant pointer is declared as follows :

Data_Type * const Pointer_Name;
eg,
int *const ptr; //constant pointer to integer

 

Let’s see the below example code when you will compile the below code to get the compiler error.

#include<stdio.h>
int main(void)
{
    int var1 = 10, var2 = 20;
    //Initialize the pointer
    int *const ptr = &var1;
    //Try to modify the pointer value
    ptr = &var2;
    printf("%d\n", *ptr);
    return 0;
}

 

Pointer to a constant:

In this scenario the value of the pointed address is constant which means we can not change the value of the address that is pointed by the pointer.

A constant pointer is declared as follows :

Data_Type  const*  Pointer_Name;
eg,
int const *ptr// pointer to const integer

 

Let’s take a small code to illustrate a pointer to a constant:

#include<stdio.h>

int main(void)
{
    int var1 = 100;
    // pointer to constant integer
    const int* ptr = &var1;
    
    //try to modify the value of pointed address
    *ptr = 10;
    
    printf("%d\n", *ptr);
    return 0;
}

Q) What is the FILE pointer?

Answer:

A File pointer is a pointer that is used to handle and keep track of the files being accessed. A new data type called “FILE” is used to declare the file pointer. This data type is defined in stdio.h file. The file pointer is declared as FILE *fptr. Where ‘fptr’ is a file pointer.

Check this Article, Learn file handling in a few hours.

 

Q) Why are void pointers used?

Answer:

A very important feature of the void pointer is reusability. Using the void pointer we can store the address of any object and whenever required we can get back the object through the indirection operator with proper casting.

Let’s see an example code,

#include <stdio.h>

int main()
{
    void *pvData;
    int iData = 10;
    char cData = 'A';
    float fData = 27.6;

    //Assigning address of character
    pvData = &cData;
    //dereferencing void pointer with character typecasting
    printf("cData = %c\n\n",*((char*)pvData));

    //Assigning address of integer
    pvData = &iData;
    //dereferencing void pointer with integer typecasting
    printf("iData = %d\n\n",*((int *)pvData));

    //Assigning address of float
    pvData = &fData;
    //dereferencing void pointer with float typecasting
    printf("fData = %f\n\n",*((float *)pvData));

    return 0;
}

Output:

void pointer

Explanation: In the above code, pvData is a void pointer. Using it I store the address of the different variables (float, int, and char) and after that get back their values using the indirection operator and proper typecasting.

You can see in the example code, how a single pointer is dealing with different types of variables. This is a very interesting feature of the void pointer that makes the programmer helpless to use the void pointer.

 

Q) What is the advantage of pointers in C?
Answer:

There following advantages of the pointer in C programming.

  • We can access the memory location with the help of C Pointers.
  • With the help of pointers, we can efficiently pass the structure. It helps to reduce stack usage.
  • We can access the elements of an array with the help of C Pointers.
  • Pointers are used for dynamic memory allocation using the memory management function.
  • It is used in complex data structures like linked lists, trees, etc.
  • Using the pointer we can jump from one application to another application.

 

Q) Can math operations be performed on a void pointer?

Answer:

According to the C standard arithmetic operation on void pointers is illegal which means the C standard doesn’t allow pointer arithmetic with void pointers. However, In GNU C, addition and subtraction operations are supported on void pointers to assume the size of the void is 1.

Let’s see an example

#include<stdio.h>

int main()
{
    int aiData[3] = {100, 200,300};
    void *pvData = &aiData[1]; //address of 200
    
    pvData += sizeof(int);
    
    printf("%d", *(int *)pvData);
    
    return 0;
}

Output: 300 or compiler error.

Explanation: When we compile the code then some compiler throws the compiler error but some compiler compiled the code and prints 300 as output to assume the size of void 1.

Note: Don’t perform the arithmetic operation on the void pointer. As per the C standard sizeof is not applicable to void but in GNU C we can calculate the size of the void and sizeof operator returns 1.

 

Q) What is the advantage of a void pointer in C?

Answer:

There are the following advantages of a void pointer in c.

  • Using the void pointer we can create a generic function that can take arguments of any data type. The memcpy and memmove library functions are the best examples of the generic function, using these functions we can copy the data from the source to the destination.
  • We know that void pointer can be converted to another data type that is the reason malloc, calloc or realloc library function return void *. Due to the void * these functions are used to allocate memory to any data type.
  • Using the void * we can create a generic linked list. For more information see this link: How to create generic Link List.

 

Q) What are dangling pointers?

Answer:

Generally, daggling pointers arise when the referencing object is deleted or deallocated, without changing the value of the pointers. It creates a problem because the pointer is still pointing to the memory that is not available. When the user tries to dereference the daggling pointers then it shows the undefined behavior and can be the cause of the segmentation fault.

For example,

#include<stdio.h>
#include<stdlib.h>


int main()
{
    int *piData = NULL;
    //creating integer of size 10.
    piData = malloc(sizeof(int)* 10);
    //make sure piBuffer is valid or not
    if (piData == NULL)
    {
        // allocation failed, exit from the program
        fprintf(stderr, "Out of memory!\n");
        exit(1);
    }
    //free the allocated memory
    free(piData);
    
    //piData is dangling pointer
    *piData = 10;
    
    printf("%d",*piData);
    
    return 0;
}

OutPut: Undefined Result

In simple words, we can say that a dangling pointer is a pointer that is not pointing to valid memory. So if we access these pointers then the behavior of the program will undefine.

 

Q) What does it mean when a pointer is used in an if statement?

Answer:

It is a good habit to check the pointer in if condition before using it. It prevents code crashing. A pointer can be used in an if, while, for, or do/while statement, or in any conditional expression.

Let’s see an example code,

if ( p )
{
    /*Run when valid address */
}
else
{
    /*When NULL pointer*/
}

 

Q) What is Indirection or Dereference Operator ( * )?

Answer:

It is a unary operator that is used in the declaration of the pointer and accesses a value indirectly, through a pointer.  The operand of the indirection operator should be a pointer and the result of the operation is value addressed by the operand (pointer).

Let see an example,

int *iPtr; // Use of indirection operator in the declaration of pointer

a = *iPtr; //Use of indirection operator to read the value of the address pointed by the pointer

*iPtr = a; //Use of indirection operator to write the value to the address pointed by pointer

 

Q) What is the address of operator ( &)?

Answer:

It is also a unary operator and gives the address of the operand. According to C standard “The operand of the unary & operator shall be either a function designator or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier”.

Let see an example,

int data = 0; // declaration  of integer variable

&data  => Give the address of the data

int *pData ; // declaration  of pointer

&pData => Give the address of the pointer

 

Q) What is segmentation fault in C?

Answer:

A segmentation fault is a common problem that causes programs to crash. A core file (core dumped file) also associated with a segmentation fault that is used by the developer to finding the root cause of the crashing (segmentation fault).

Generally, the segmentation fault occurs when a program tried to access a memory location that is not allowed to access or tried to access a memory location in a way that is not allowed (tried to access read-only memory).

 

Q) What is the difference between Segmentation fault and Bus error?

Answer:

In the case of segmentation fault, SIGSEGV (11) signal is generated. Generally, a segmentation fault occurs when the program tries to access the memory to which it doesn’t have access to.

Below, I have mentioned some scenarios where SIGSEGV signal is generated.

  • When trying to de-referencing a NULL pointer.
  • Trying to access memory which is already de-allocated (trying to use dangling pointers).
  • Using uninitialized pointer(wild pointer).
  • Trying to access memory that the program doesn’t own (eg. trying to access an array element out of array bounds).

In case of a BUS error, SIGBUS (10) signal is generated. The Bus error issue occurs when a program tries to access an invalid memory or unaligned memory. The bus error comes rarely as compared to the segmentation fault.

 

Q) What are the common causes of the segmentation fault in C?

Answer:

There are many reasons for the segmentation fault, here I am listing some common causes of the segmentation fault.

  • Dereferencing NULL pointers.
  • Tried to write read-only memory (such as code segment).
  • Trying to access a nonexistent memory address (outside process’s address space).
  • Trying to access memory the program does not have rights to (such as kernel structures in process context).
  • Sometimes dereferencing or assigning to an uninitialized pointer (because might point an invalid memory) can be the cause of the segmentation fault.
  • Dereferencing the freed memory (after calling the free function) can also be caused by the segmentation fault.
  • A stack overflow is also caused by the segmentation fault.
  • A buffer overflow (try to access the array beyond the boundary) is also a cause of the segmentation fault.

 

Q) What is the stack overflow?

Answer:

If your program tries to access beyond the limit of the available stack memory then stack overflow occurs. In other words, you can say that a stack overflow occurs if the call stack pointer exceeds the stack boundary.

If stack overflow occurs, the program can crash or you can say that segmentation fault that is the result of the stack overflow.

 

Q) What is the cause of the stack overflow?

Answer:

In the embedded application we have a little amount of stack memory as compare to the desktop application. So we have to work on embedded application very carefully either we can face the stack overflow issues that can be a cause of the application crash.

Here, I have mentioned some causes of unwanted use of the stack.

  • Improper use of the recursive function.
  • Passing to many arguments in the function.
  • Passing a structure directly into a function.
  • Nested function calls.
  • Creating a huge size local array.

 

Q) What is a Function Pointer?

Answer:

function pointer is similar to the other pointers but the only difference is that it points to a function instead of a variable. In another word, we can say that a function pointer is a type of pointer that store the address of a function and these pointed function can be invoked by function pointer in a program whenever required.

 

Q) How to declare a pointer to a function in C?

Answer:

The syntax for declaring function pointer is very straightforward. It seems difficult in beginning but once you are familiar with function pointer then it becomes easy.

The declaration of a pointer to a function is similar to the declaration of a function. That means the function pointer also requires a return type, declaration name, and argument list. One thing that you need to remember here is, whenever you declare the function pointer in the program then the declaration name is preceded by the * (Asterisk) symbol and enclosed in parenthesis.

For example,

void ( *fpData )( int );

For a better understanding, let’s take an example to describe the declaration of a function pointer in the C program.
e.g,

void ( *pfDisplayMessage) (const char *);

In the above expression, pfDisplayMessage is a pointer to a function taking one argument, const char *, and returns void.

When we declare a pointer to function in c then there is a lot of importance of the bracket. If in the above example, I remove the bracket, then the meaning of the above expression will be change and it becomes void *pfDisplayMessage (const char *). It is a declaration of a function that takes the const character pointer as arguments and returns a void pointer.

 

Q) Where can the function pointers be used?

Answer:

There are a lot of places, where the function pointers can be used. Generally, function pointers are used in the implementation of the callback function, finite state machine and to provide the feature of polymorphism in C language …etc.

 

Q) Can we perform arithmetic operation on pointers?

Answer:

Yes. See this article for more detail: Arithmetic operation on pointers.

 

Q) What is the difference between pointer to an array and array of pointers?

Answer:

See this article, Click here.

 

Q) What is the difference between pointer and array in C?

Answer:

See this article, Click here.

 

Q) What is a normalized pointer, how do we normalize a pointer?

Answer:

It is a 32bit pointer, which has as much of its value in the segment register as possible. Since a segment can start every 16bytes so the offset will have a value from 0 to F. for normalization convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset address. Given a pointer 500D: 9407, we convert it to a 20bitabsolute address 549D7, Which is then normalized to 549D:0007.

 

Q) What is an array of pointers?

Answer:

It is basically an array of the pointer variables. It is also known as pointer arrays.

Declaration of an array of pointers:

data_type *arrar_name[array_size];

Example,

int *arr[5];

Here “arr” is an array of  5 integer pointers.

See this article for more detail, click here.

 

Q) Is the allocated space within a function automatically deallocated when the function returns?

Answer: 

No, you have to manually deallocate the allocated memory. The allocated memory is managed my memory management function and memory allocates from the heap memory.

See the Below articles,

 

Q) Are the expressions *ptr ++ and ++*ptr same ?

Answer: 

Both expressions are different. Let’s see a sample code to understand the difference between both expressions.

#include <stdio.h>

int main(void)
{
    int aiData[5] = {100,200,300,400,500};
    
    int *piData = aiData;
    
    ++*piData;
    
    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);
    
    return 0;
}

Output: 101 , 200 , 101

Explanation:
In the above example, two operators are involved and both have the same precedence with right-to-left associativity. So the above expression ++*p is equivalent to ++ (*p). In another word, we can say it is a pre-increment of value and output is 101, 200, 101.

 

#include <stdio.h>

int main(void)
{
    int aiData[5] = {100,200,30,40,50};
    
    int *piData = aiData;
    
    *++piData;
    
    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);
    
    return 0;
}

Output: 100, 200, 200

Explanation:
In the above example, two operators are involved and both have the same precedence with the right to left associativity. So the above expression *++p is equivalent to *(++p). In another word you can say it is pre-increment of address and output is 100, 200,200.

 

Q) What are the advantages of using an array of pointers to string instead of an array of strings?

Answer: 

An array of pointers to string is useful when sorting the strings, we need to swap only pointers instead of swapping the whole string which helps in the efficient use of memory and time.

 

Q) What is the memory leak in C?

Answer:

memory leak is a common and dangerous problem. It is a type of resource leak. In C language, a memory leak occurs when you allocate a block of memory using the memory management function and forget to release it.

int main ()
{
    char * pBuffer = malloc(sizeof(char) * 20);
    /* Do some work */
    return 0; /*Not freeing the allocated memory*/
}

Note: once you allocate a memory than allocated memory does not allocate to another program or process until it gets free.

 

Q) What is the return value of malloc (0)?

Answer:

If the size of the requested space is zero, the behavior will be implementation-defined. The return value of the malloc could be a null pointer or it shows the behavior of that size is some nonzero value. It is suggested by the standard to not use the pointer to access an object that is returned by the malloc while the size is zero.

 

Q) How to access pointer inside the structure in C?

Answer: 

See this article, Click here.

 

Q) How to use a function pointer in structure in C?

Answer:

See below articles,

 

Q) What is the use of a double pointer (pointer to pointer) in C?

Answer:

There is a lot of application of double-pointer in C language but here I am describing one important application of double-pointer. If you want to create a function to allocate the memory and you want to get back the allocated memory from the function parameter, then you need to use the double-pointer in that scenario. See the below code,

#include<stdio.h>
#include<stdlib.h>


void AllocateMemory(int **pGetMemory,int n)
{
    int *p = malloc(sizeof(int)*n);
    if(p == NULL)
    {
        *pGetMemory = NULL;
    }
    else
    {
        *pGetMemory = p;
    }
}


int main()
{
    int *arr = NULL;
    int len = 10;
    int i =0;
    //Allocate the memory
    AllocateMemory(&arr,len);
    if(arr == NULL)
    {
        printf("Failed to allocate the memory\n");
        return -1;
    }
    //Store the value
    for(i=0; i<len; i++)
    {
        arr[i] = i;
    }
    //print the value
    for(i=0; i<len; i++)
    {
        printf("arr[%d] = %d\n",i, arr[i]);
    }
    //free the memory
    free(arr);
    return 0;
}

Output:

double pointer application

 

Q) Can we have a volatile pointer?

Answer:

Yes, we can create a volatile pointer in C language.

int * volatile piData; // piData is a volatile pointer to an integer.

 

Q) When should we use pointers in a C program?

Answer:

  • To pass a large structure like server request or response packet.
  • To implement the linked list and binary trees.
  • To play with GPIO or hardware register.
  • To get the address or update value from the function (call by reference)
  • To create a dynamic array.
  • To create a call-back function using the function pointer.

Note: Besides it, lots of places where the need to use the pointer.

 

Q) What is the advantage of a void pointer in C?

Answer:

You can see this article, click here.

 

Q) Can we use the const qualifier with pointers in C?

Answer:

Yes, we can use const and pointer together. I have created a list of some interview questions related to the const and pointer. See the list, click here.

 

Q) What is the difference between memory leak and dangling pointer?

Answer:

 See this article, Click here.

 

Note:  If you want an explanation of the below programs, then write a comment in the comment box I want to listen to your thoughts.

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

void fun(int *p)
{
  int q = 40;
  p = &q;
}

int main()
{
  int data = 27;
  int *ptr = &data;

  fun(ptr);

  printf("%d", *ptr);

  return 0;
}

Output:

27

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

void fun(int **p)
{
  int q = 40;
  *p = &q;
}

int main()
{
  int data = 27;
  int *ptr = &data;

  fun(&ptr);

  printf("%d", *ptr);

  return 0;
}

Output:

Undefined behavior

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

void fun(int **p)
{
  static int q = 40;
  *p = &q;
}

int main()
{
  int data = 27;
  int *ptr = &data;

  fun(&ptr);

  printf("%d", *ptr);

  return 0;
}

Output:

40

 

If you love online courses and want to learn C programming, you can check the below courses it will help.

 

Q) What is the output of the below program? Assume sizeof an integer and a pointer is 4 byte.

Answer:

#include <stdio.h>

int main()
{
   int (*ptr)[5][10];

   printf("%d",  sizeof(*ptr));

   return 0;
}

Output:

200

 

Q) What is the output of the below program?

Answer:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** ptr)
{
    ptr++;
}

int main()
{
    char *str = malloc(20*sizeof(char));
    if(str == NULL)
        return -1;
    strcpy(str, "Aticleworld");

    fun(&str);

    puts(str);

    free(str);

    return 0;
}

Output:

Aticleworld

 

Q) What is the output of the below program?

Answer:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** ptr)
{
    (*ptr)++;
}

int main()
{
    char *str = malloc(20*sizeof(char));
    if(str == NULL)
        return -1;
    strcpy(str, "Aticleworld");

    fun(&str);

    puts(str);

    free(str);

    return 0;
}

Output:

ticleworld

 

Q) What is the output of the below program?

Answer:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i;
    int *ptr = (int *) malloc(5 * sizeof(int));
    if(ptr == NULL)
    {
        return -1;
    }

    for (i=0; i<5; i++)
    {
        *(ptr + i) = i;
    }

    printf("%d ", *ptr++);
    printf("%d ", (*ptr)++);
    printf("%d ", *ptr);
    printf("%d ", *++ptr);
    printf("%d ", ++*ptr);

    return 0;
}

Output:

0 1 2 2 3

 

Q) What is the output of the below program?

Answer:

#include <stdio.h>

int f(int x, int *py, int **ppz)
{
    int y, z;
    **ppz += 1;
    z  = **ppz;
    *py += 2;
    y = *py;
    x += 3;
    return x + y + z;
}

void main()
{
    int c, *b, **a;
    c = 4;
    b = &c;
    a = &b;
    printf( "%d", f(c,b,a));

    return 0;
}

Output:

19

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    int acData[2][3] = {{1, 2, 15}, {200, 215, 400}};
    int(*pcData)[2][3] = &acData;

    printf("%d\t", ***pcData);
    printf("%d\t", ***(pcData + 1));
    printf("%d\t", **(*pcData + 1));
    printf("%d\t", *(*(*pcData + 1) + 2));

    return 0;
}

Output:

1, Garbage Value, 200, 400

 

Q) What is the output of the below program?

Answer:

#include <stdio.h>

int main()
{
    char *pcData="aticleworld";

    printf("%c ",6[pcData]);

    return 0;
}

Output:

w

 

Q) What does the below declaration mean?

int (*ptr)[5];

Answer:

ptr is a pointer to an array of 5 integers.

 

Q) Is there any issue in the below program?

#include "stdlib.h"

int main()
{
    int *ptr1;
    int **ptr2;
    int **ptr3;

    ptr1 = (int*)malloc(sizeof(int));
    ptr2 = (int**)malloc(10*sizeof(int*));
    ptr3 = (int**)malloc(10*sizeof(int*));

    free(ptr1);
    free(ptr2);
    free(*ptr3);

    return 0;
}

Output:

No issue.

 

Q) What is the output of the below program?

Answer:

#include <stdio.h>

int main()
{
    void *ptr;

    ptr = (void*)0;

    printf("%u",sizeof(ptr));

    return 0;
}

Output:

size of the void pointer.

 

Q) What is the output of the below program?

Answer:

#include "stdio.h"

typedef void (*fPtr)(int);

void display(int a)
{
    printf("%d\n",a);
}

int main()
{
    fPtr fPtr1 = NULL, fPtr2 = NULL;

    fPtr1 = &display;
    fPtr2 = display;

    (*fPtr1)(10);

    fPtr2(10);

    return 0;
}

Output:

10
10

 

Q) ‘ptr’ is a pointer to a data type. The expression *ptr++ is evaluated as?

Answer:

*(ptr++);

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    short a = 320;
    char * ptr = NULL;

    ptr = (char * )&a;
    printf("%d", * ptr);

    return 0;
}

Output:

64

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    printf("%u",sizeof(NULL));

    return 0;
}

Output:

The size will same as other pointers.

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    int * ptr = NULL;

    printf("%d",*ptr);

    return 0;
}

Output:

Behaviour defined when you dereference null pointer your program will crash.

 

Q) What is the output of the below program?

Answer:

#include <stdio.h>

int main()
{
    int data = 24;
    int *ptr = NULL;
    int **ptr1 = NULL;

    ptr = &data;
    ptr1 = &ptr;

    printf("%d\n", *ptr );
    printf("%d\n", **ptr1);

    return 0;
}

Output:

24
24

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    int i = 3;
    int *j;
    int **k;
    j = &i;
    k = &j;
    k++;

    printf("%d ",**k);

    return 0;
}

Output:

Run time error.

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    register int data = 10;
    int *ptr = NULL;

    ptr = &data;
    printf("%d ",*ptr);

    return 0;
}

Output:

compilation issue.

 

Q) What is the output of the below program? The assumed size of char, int, and double is 1,4,8.

Answer:

#include<stdio.h>

int main()
{
    int a, b, c;
    char *p = 0;
    int *q = 0;
    double *r = 0;
    
    a = (int)(p + 1);
    b = (int)(q + 1);
    c = (int)(r + 1);
    
    printf("%d %d  %d",a, b, c);
    
    return 0;
}

Output:

1,4,8

See this Article, Addition of pointer in C.

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>
int main()
{
    int *ptr;
    
    *ptr = 5;
    
    printf("%d", *ptr);
    
    return 0;
}

Output:

Run time error.

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

int main()
{
    int data = 27;
    int *ptr = &data;
    
    printf("%u %u", *&ptr, &*ptr);
    
    return 0;
}

Output:

Address of data variable, Address of data variable

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>

struct node
{
    int a, b, c;
};

int main()
{

    struct node data = {3, 5, 6};
    struct node *ptr = &data;

    printf("%d\n", *((int*)ptr + 1 + (3-2)));

    return 0;
}

Output:

6

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>
int main()
{
    int data = 5, data1 = 6;

    int *ptr = &data1;

    printf(" %d ", data  **ptr);

    return 0;
}

Output:

30

 

Q) What is the output of the below program?

Answer:

#include<stdio.h>
int main()
{
    char *ptr = "Aticleworld";
    void *vptr;
    vptr = &ptr;
    printf("%s",*(char **)vptr);
    return 0;
}

Output:

Aticleworld

 

 

Q) Which type of pointer is the most convenient way of storing the raw address in C programming?

Answer

void pointer

 

Q) Is there any issue in the below code?

void fun()
{
    int* restrict p1 = &data1;
    
    int* restrict p2 = &data2;
    
    p1 = p2;
    
    /*Doing some work*/
}

Answer:

Undefined Behaviour

Explanation: Assignment from one restricted pointer to another is undefined behavior within the same scope. You can read more about “restrict keyword in C“.

Q) What is the output of the below code?

/*
C program to swap two numbers
without using a temporary variable?.
*/
#include<stdio.h>
#include<stdint.h>

int main()
{
    int16_t a = 0x1234;
    void *ptr = &a;

    printf("%d\n",*(int16_t*)ptr);

    *((int32_t*)ptr) = 3;

    printf("%x\n",*(int16_t*)ptr);

    return 0;
}

 

 

If you want to add another C pointer interview question to this list, then you are welcome. I will publish the mentioned pointer interview questions in C with your name. If you have any other suggestions for this C pointers questions list, then please write in the comment box or you can directly email admin@aticleworld.com.

In the Last, I will also try to create a free eBook on pointer interview questions in C (Pointer interview questions in c pdf).

Recommended Articles for you:



One comment

  1. Q) What is the meaning of the below declarations?
    1. const int a;
    2. int const a;
    3. const int *a;
    4. int * const a;
    5. int const * a const;
    Answer:
    The “a” is a constant integer.
    Similar to first, “a” is a constant integer.
    Here “a” is a pointer to a const integer, the value of the integer is not modifiable, but the pointer is not modifiable.
    Here “a” is a const pointer to an integer, the value of the pointed integer is modifiable, but the pointer is not modifiable.
    Here “a” is a const pointer to a const integer that means the value of the pointed integer and pointer both are not modifiable.

    The third option is confusing.

Leave a Reply

Your email address will not be published. Required fields are marked *