top of page

C Pointers Tricky Questions and Answers

Not understanding the concept of the Pointers can result in missing great opportunities.


Some famous companies have asked tricky questions related to pointers in their fintech coding interview questions. The questions on pointers in C that were asked were difficult to answer for those who don’t have a proper understanding of it.


If you are not having an understanding of questions on pointers in C, then don’t worry. This is the piece of content that you all need.


After reading the whole blog, you will be able to answer all the fintech interview questions that are based on Pointers. So, let’s get started with the concept of Pointers.



What are Pointers in C?

Those who are newbies to programming might be facing problems in understanding the concept of Pointers in C. However, it is not a difficult term to understand.


The Pointers in C are used for storing the memory address of a particular variable as its value. When you declare a pointer, then you will have to give it a particular address of the variable by which it will be storing its value.


When you are declaring a pointer in the program, then you will have to use the symbol of an asterisk which is “*”. After declaring the pointer, it will look like: int* ptr = &variablename;


There are other ways too through which you will be able to declare the pointer: int *ptr and int * ptr.


So, here is the code through which you can get to know how the pointers are declared.


int age=20;

int* ptr= &age;


When you will print the statement of printf("%p\n", ptr); then you will get the memory address of the variable age and if you print the statement *ptr, then you will get the value of age which is 20.


You will have to run the code to properly understand when will be the memory address printed and when will be the value printed. A lot of candidates get stuck with these types of tricky questions on pointers in C.


So, run the code on your editor and check for the output that you get. Once you are done with it, then let’s take a look at some most asked questions of Pointers in C

.

Some Important Questions on Pointers in C

If you want to make yourself an expert in the topic of Pointers, then you will have to practice as many questions as you can. Without practicing you will be not able clear the fintech interview questions.

Question 1: What will be the output for the given question?

#include <stdio.h>

int main()

{

char *ptr1 = "helloworld";

printf(ptr1 + 5);

return 0;

}


Output: world.


Explanation: The ptr1 is a pointer that has the value of helloworld. Now, for printing the output, we are calling the ptr with its address. So, the output that will be printed is “world” as it starts from the fifth index. This is how we will get the output of the “world”.


Question 2: Find the output for the given question.


#include<stdio.h>

int main()

{

char *ptr1;

char string1[] = "learn C";

ptr1 = string1;

ptr1 += 2;

printf("%s",ptr1);

return 0;

}

Output: arn C


Explanation: We have declared a ptr1 and string1. Now, we have declared the value of string 1 to the ptr1. After it, we incremented the ptr1 by 2. So, once we call the ptr1, then it will start printing the output from the index 2. This is why we will get the arc C as the output.


Question 3: What will be the output for the given question.

#include<stdio.h>

int main()

{

int *ptr;

printf("%d", &ptr);

return 0;

}


Output: Garbage Value.


Explanation: We have initialized the pointer and after it, we are printing the &ptr which will give the address of the pointer where it has been stored. So, for example, you will get the output as -1 7 6 1 6 0 9 3 8 4.

Question 4: Find the output for the given code.

#include <stdio.h>

int main()

{

int age = 20;

int *ptr = &age;

printf("%d", *ptr);

return 0;

}

Output: 20.


Explanation: We have declared a variable which is age having the value 20. After it, we initialized and declared the pointer which is storing the memory address of the age. So, when we will print the *ptr, then the value of age which is 20 will be printed.


Question 5: Find the output for the given code.

#include <stdio.h>

int main()

{

int number = 30;

int *ptr = &number;

printf("%d", ptr);

return 0;

}

Output: Garbage Value.


Explanation: We have declared a variable which is a number having the value 30. After it, we initialized and declared the pointer which is storing the memory address of the number. But, here is a catch, we are not printing the value of the *ptr, but we are printing the value of the ptr which is the address where we are storing the pointer. So, because of this, we will be getting the output of garbage value which will look like this: 6 3 1 2 4 0 8 5 2. Learn more...


Recent Posts

See All

Comments


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2023 by Train of Thoughts. Proudly created with Wix.com

bottom of page