© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

11. Can you add pointers together? Why would you?

Ans:
No, you can’t add pointers together. If you live at 1332 Lakeview Drive, and your neighbor lives at 1364 Lakeview, what’s 1332+1364? It’s a number, but it doesn’t mean anything. If you try to perform this type of calculation with pointers in a C program, your compiler will complain.
The only time the addition of pointers might come up is if you try to add a pointer and the difference of two pointers.

Ex - 1:  Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Ans:
                        (char*(*)( )) (*ptr[N])( );

Ex - 2 What is wrong with the following code? 
int *foo()
{
int *s = malloc(sizeof(int)100);
assert(s != NULL);
return s;
}

Answer & Explanation:

assert macro should be used for debugging and finding out bugs. The check s != NULL is for error/exception handling and for that assert shouldn’t be used. A plain if and the corresponding remedy statement has to be given.

Ex - 2 What is the output? 
void main()
{
int *i = 0x400;  // i points to the address 400
*i = 0;              // set the value of memory location pointed by i;
}

Ans:
Undefined behavior

Explanation:

The second statement results in undefined behavior because it points to some location whose value may not be available for modification.  This type of pointer in which the non-availability of the implementation of the referenced location is known as 'incomplete type'.


SLogix Student Projects
bottom