Thursday, 28 April 2016

Opaque data type:

When concrete implementation is not exposed to the user then it is called as opaque data type.

What is concrete implementation?

Consider you were writing one library and exposing to the user. Let the library be “Calculator”.  So when user wants to use your library one has to access from their source code.
Consider the following sample code,

int main()
CALCULATOR *l_calculator_object; // Opaque Data type
openCalculator(l_calculator_object); // Library function call
closeCalculator(l_calculator_object); // Library function call

Here library haven’t exposed the implementation of Calculator class. Implementation of Calculator class is present in the library. Here Calculator is the opaque data type.

At library, they may define the CALCULATOR as follows

typedef  calculator CALCULATOR;

So what is opaque pointer then?

Pointer to the opaque data type is called as an opaque pointer
Here l_calculator_object is an opaque pointer

opaque pointer:

Pointer variable defined with opaque data type is called opaque pointers.

Consider another example,

typedef struct s s_t;

Here Implementation details of struct s is hidden from the end users.
The end user can just declare and use the struct for further communication to the functionality module
FILE pointer in c is also one of the opaque pointer.

Eg: FILE *fp;
fclose(fp); // Use of opaque pointer at client side
Objective of the concept

To hide the implementation details of the data structure.


Changes in the type will not affect the client code (For Eg if we add one more member in the structure it will not affect the client code. so client code no need to be compiled again. Only the library has to be compiled.

Information hiding

Information hiding is nothing but hiding the implementation details to the end user. Here opaque pointer or opaque data type is one of the information hiding.
Why opaque data type is one of the information hiding technique?
Consider the FILE pointer in c file library. Here we know FILE is an structure or type definition of some structure.

FILE *l_file_pointer;

Here we don’t know the members which is defined inside that structure, it means they hidden information inside that structure. So it is called as information hiding.

Transparent data type

Transparent data type is opposite to the opaque data type. Here information about the structure like member of the structure, data type of the member will be open to the programmer.
Consider the following code.

struct myStruct
int a;
int main()
myStruct l_obj;
Here myStruct is an transparent data type.

Declaring local object for the opaque data type

You cannot declare the object for the opaque data type. This is because, when local object is created for some structure, then during compilation compiler will expect for the concrete implementation of the structure. Since concrete implementation for the structure is not available, you cannot create an object for the opaque data type.

Calculating the size of opaque data type

No, you cannot calculate the size of the opaque data type.

Why you cannot calculate the size of the opaque data type?
Since you don’t have the concrete implementation of the structure, you cannot create the object to that structure. So obviously you cannot calculate the size of the opaque data type.

Advantages of opaque pointer

1.       We can hide the implementation of the class to the user of the class
2.       Any change the structure of the class will not affect the user of the class because client is using only an opaque pointer
3.       Whenever class structure is changed, user of the class no needs to change the library.

Disadvantages of opaque pointer

Class cannot be accessed directly. Everything needs to be accessed as member function.

Application of opaque pointer:

Opaque data type is mainly used in the operating system related activity like writing content in file, reading a content of file etc.,

Tuesday, 26 April 2016

C++ interview questions

Use the argv[0] parameter. It  will print the executable name.


int main(int argc, char *argv[])

   using namespace std;
   printf("\n ");
   printf("\n  :: %s ", argv[0] );
   return 0;


Monday, 25 April 2016

The answers is yes.  But we may studied that the size of pointer is common for all int pointer,char pointer,double poiter.These are data pointers.

Sizeof data pointer will be different from function pointer

int main()


 int *a;// data pointer

 int (*my_function)(); // Function pointer

 printf(“%u is not equal to %u”, sizeof a,sizeof my_function);

 printf(“Disclaimer: This is purely Architecture dependent”);


Reason behind this info:
System will store the code and data in different memory. Eg : Havard Architecture
As per the standard result is undefined. On x86 architecture it will return the value of the EAX register.
EAX register will contain the result of the last executed instruction. Answer depends upon the platform which you were using.
But mostly it returns the result of the last executed instruction


int express()
   int a=5+5;

int donothing()


int declarevariable()
   int a=9;

int main()
   using namespace std;

   cout << '\n' << express();
   cout << '\n' << donothing();
   cout << '\n' << declarevariable();

   return 0;

NULL is an pre-processor macro which may contain the following definition.

#define NULL 0

‘\0’ is an ascii character and equivalent ascii value is 0

“\0”  is an string which is null terminated.

int main()

   char *null_1=NULL;
   char *null_2='\0';
   char *null_3="\0";

   return 0;


Did you remember in school days that variables  will be allowed to be declare at the beginning of the block only. If so then you have used a c90 standard  or earlier. After c99 standard variables were allowed to be declared at anywhere in the block.
Because unsigned float does not have an equivalent machine code to be executed by the CPU. I.e., Most of the hardware implementation does not support this feature
No standard defines that floating point can be negative.

Sunday, 24 April 2016


                Usually data members cannot be modified when function is declared as const. If there is situation that even if function is declared data member need to be modified due to unavoidable situation. To achieve this declare the data member with mutable specifier.


class test
   int a;
   mutable int b;

   void updateConstFunc()const

      // a=1; ==> Not possible since function is const
      b=2; //==> possible since mutable specified

int main()
   test t;
   return 0;