rahul


{ City } belagavi
< Country > india
* Profession *
User No # 121359
Total Questions Posted # 0
Total Answers Posted # 3

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 1
Users Marked my Answers as Wrong # 1
Questions / { rahul }
Questions Answers Category Views Company eMail




Answers / { rahul }

Question { Hexaware, 14746 }

#include

main()

{

struct xx

{

int x=3;

char name[]="hello";

};

struct xx *s;

printf("%d",s->x);

printf("%s",s->name);

}


Answer

There is no syntax error.

Pointer s is declared but never initialized.

Using uninitialized pointer , trying to access members(x and name) of structure results in invalid access of memory.

Hence segmentation fault.

Is This Answer Correct ?    0 Yes 0 No

Question { 10525 }

main()

{

int i=_l_abc(10);

printf("%d\n",--i);

}

int _l_abc(int i)

{

return(i++);

}


Answer

Post increment - perform operation first , then increment

In function call _l_abc its post increment, so after value 10 to be returned is decided , local variable i is increment , its i in function.

Variable i in _l_abc is different than i in main.

Post decrement : decrement first then perform operation.

In main its pre decrement , returned 10 is decremented to 9, then printed.

Now , unless compiler does not throw error of beginning function name with _ , 9 is printed.

Is This Answer Correct ?    1 Yes 1 No


Question { 9291 }

char *someFun1()

{

char temp[ ] = “string";

return temp;

}

char *someFun2()

{

char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};

return temp;

}

int main()

{

puts(someFun1());

puts(someFun2());

}


Answer

Variables are in function calls, hence on heap space. Variable address is being returned by function and when function returns the allocated space is freed.

Now accessing returned address will result in : segmentation fault.

As address no longer allocated to function, results in invalid address accessed by process.
As variable address does not exist ( hence variable address also , as heap is freed).

Is This Answer Correct ?    0 Yes 0 No