UrbanPro
true

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Some interview questions and answers for fresher level on Pointers

Shiladitya Munshi
25/11/2016 0 0
What is a void pointer?
 
Void pointer is a special type of pointer which can reference or point to any data type.  This is why it is also called as Generic Pointer.
 
As a void pointer can point to any data type, neither it is possible to dereference a void pointer, nor is it possible to do any arithmetic operations on void pointer. Hence an explicit type casting is absolutely must for using a void pointer.
 
The best use of void pointer can be exhibited in the case of malloc and calloc. This is discussed in details in this post.
 
What is a null pointer?
 
A null pointer is also a special type of pointer which points definitively to nothing.
As per official C language description, every valid type of pointer has a special value called “Null Pointer” and this value is always distinguishable from all other pointer values and it is guaranteed to compare unequal to a pointer of any object or function.  
 
Hold it for a second! So what you are saying is there are different versions of null pointers for every pointer type. Is it so?
 
No. We cannot call it as a “different versions” rather their internal values are different. But as a programmer, we need not to worry about it because compilers take care of and track the internal values (may be differently depending on the compiler types)
 
So why don’t you say that a null pointer is a pointer which is not initialized yet?
 
I cannot go with this idea because an uninitialized pointer may point to any thing. There might be an uninitialized char pointer, an uninitialized int pointer and so on. But a null pointer is definitely something which is there not to point anything.
 
Ok. Now tell me what is a dangling pointer?
Dangling pointers are the pointers which point to already de allocated or de referenced memory locations. Suppose I write a piece of code like this
 
int*p;
p = (int*)malloc(n*sizeof(int);
----
----
free(p);
 
In this piece of code, after allocating some memory to p, we are de allocating the memory which was initially pointed by p. Hence now onwards, p acts as dangling pointer.
 
How to rectify this?
 
In this case, after de allocation that is after using free() we should set the pointer p to null as p = NULL   
 
Could you please exhibit any other source of dangling pointer problems?
 
Well, there might be other sources also, like
Suppose I have declared int*p, and within a specific scope, I have defined another integer x as 10; and then within that specific scope I am making the pointer p to hold the base address of x. Now outside this specific scope the p will act as a dangling pointer. I can write the code as
 
void foo (void)
{
   int *p;
   {
          {
int x = 10;
                 p = &x;
          }
          // here p will act as dangling pointer
   }
}
 
Another case might occur when from a function, we return an address of any variable and a corresponding pointer in the receiver section receives it, then the receiving pointer might get into dangling pointer problem, as just after the returning, as the variable has passed it scope, the memory stack (where the variable is stored currently) might be refreshed. But this phenomenon is not obvious, this might happen.
 
Fine, so how to deal with this second case?
 
To deal with this, to be in safer side, we must have the scope of the variable intact out of the function call; declaring it as static might be an option.
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Program Implementation
To implement a program, a student must understand the fundamental programming constructs of the language, as well as the design of the program. Object-oriented development involves implementing the individual...

Fundamentals of Electronic Devices
When we talk about electronic devices, then we are dealing with Semiconductors. They have properties lying in between conductors and insulators. The resistivities of Silicon and Germanium lies between...

Bitwise Operators - Binary Number System
Binary Number System A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor,...

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Fluid mechanics
Fluid Mechanics: An applied science Part AAbstractThis paper identifies fluid mechanics as an essential component of engineering and its applications in everyday life. The fundamental formula and equations...
P

Paradise

0 0
0
X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more