struct {
int b1:5;
}flag;
The size of this structure will be 4bytes. i.e one integer space is allocated.
In the above example we are using only 5 bits
struct {
int b1:5;
int b2:30;
}flag;
The size of this structure will be 8bytes . i.e 2 integer space is allocated
In the above example we are using in all 35 bits which is more then 4 bytes (4 *8 =32bits), so for 3 more bits we require one more integer
Question:
struct {
int b1:4;
int count;
int b2:5;
} dummy;
What will be the size of dummy?
2. What is the output of the following program
int main()
{
int x;
int y=(10,15);
x=10,15;
printf("%d %d",x,y);
return 0;
}
3. Now some interesting linked list interview questions
- Due to some corruption a loop got introduced in a singly linked list. How to find the loop in the linked list
- Due to some corruption 2 linked lists intersect. How to find the intersection between them.
- Linked list reversal
- How to find the center of the linked list
Linked list reversal example
#include
#include
struct mylnk
{
int data;
struct mylnk * next;
};
int populate(struct mylnk ** list, char * srcdata[], int count)
{
int i;
struct mylnk * newlist=NULL;
struct mylnk * newdata;
#ifdef DEBUG
printf("%s():%d started.. \n",__FUNCTION__,__LINE__);
#endif
*list=newlist;
for(i=1; i<=count; i++)
{
newdata= (struct mylnk *) malloc(sizeof(struct mylnk));
if(newdata != NULL)
{
newdata->data = atoi(srcdata[i]);
newdata->next = NULL;
if( newlist == NULL )
{
newlist =newdata;
*list=newlist;
}
else
{
newlist->next =newdata;
newlist = newlist->next;
}
}
else
{
printf("Could not allocate memory for link list member");
return -1;
}
}
return 0;
}
int display(struct mylnk * lnk)
{
#ifdef DEBUG
printf("%s():%d started.. \n",__FUNCTION__,__LINE__);
#endif
while(lnk!=NULL)
{
printf("Data:%d \n",lnk->data);
lnk=lnk->next;
}
return 0;
}
int printreverse( struct mylnk *lnk)
{
#ifdef DEBUG
printf("%s():%d started.. \n",__FUNCTION__,__LINE__);
#endif
if(lnk !=NULL)
{
printreverse(lnk->next);
}
else
{
return 0;
}
printf("The data is %d \n",lnk->data);
return 0;
}
int reverselist(struct mylnk *lnk, struct mylnk ** head)
{
#ifdef DEBUG
printf("%s():%d started.. \n",__FUNCTION__,__LINE__);
#endif
if(lnk->next !=NULL)
{
reverselist(lnk->next,head);
lnk->next->next=lnk;
lnk->next->next->next=NULL;
}
else
{
*head = lnk;
}
return 0;
}
int main(int argc, char * argv[])
{
struct mylnk * linklist=NULL, * revlinklist=NULL;
#ifdef DEBUG
printf("Link list reversal using recursion \n");
#endif
populate(&linklist, argv, (argc-1));
display(linklist);
//printreverse(linklist);
reverselist(linklist,&revlinklist);
display(revlinklist);
return 0;
}
4. You have a very large buffer, in that you have 0's at the begin, after some point in the buffer we have non zero value.
Find the position of non-zero starting value position in the buffer.
There are 2 ways which I can think of, Sequential search and binary search
5. How to make process reload its configuration.
6. Where are static variables placed
Static variables are placed in the data segment of the process.
If there are 2 static variables of same name in different functions,then ?
The variables stored as stags "variable name:function", by the compiler.
7. How to print the return address of calling function from a called function
Gcc has function by calling them you can get the return address from where this function was called from. For more on these functions: http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html
An sample program for the same is:
#include
int foo()
{
void *add = __builtin_return_address(0);
printf("I was called from %x \n", add);
return 1;
}
int main()
{
printf("Return address Testing \n");
foo();
return 0;
}
No comments:
Post a Comment