–2 votes –2 votes int main { int * str ="2017"; printf("%d", madeEasy(str)); return 0; } int maseEasy(int * p1) { int * p2 = p1; while(*++p1); return (p1-p2); } assume object of data type int occupies 2 bytes then output of the program is.............?? Programming in C non-gate + – cse23 asked Sep 10, 2016 cse23 1.4k views answer comment Share Follow See all 3 Comments See all 3 3 Comments reply srestha commented Sep 10, 2016 reply Follow Share 2 Byte 0 votes 0 votes papesh commented Sep 10, 2016 reply Follow Share i think it will run infinitely... it will terminate when int size memory location contains all 0's.. 0 votes 0 votes Arjun commented Sep 10, 2016 reply Follow Share No C code will be asked in GATE which has a compile error/warning. So, this is out of GATE scope. Those who have enough time can waste it. 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes My ans is 4 ,bcoz ptr differnce = (p1 -p2) / size of datatype = 8 /2 = 4 Tariq Husain Khan answered Sep 10, 2016 Tariq Husain Khan comment Share Follow See all 10 Comments See all 10 10 Comments reply Show 7 previous comments Arjun commented Sep 10, 2016 reply Follow Share But can we use an integer pointer to point to a string literal? What will happen to the integer alignment requirements? As per C standard this causes undefnied behaviour. 1 votes 1 votes ManojK commented Sep 10, 2016 reply Follow Share yes undefined behaviour.Is it due to n-byte aligned of int (where n=2 or 4) whereas char 1-B alignment. 0 votes 0 votes Arjun commented Sep 10, 2016 reply Follow Share In C we can use a char pointer to point to any other type. No other type pointer (not including void*) can be used for pointing to a different type. 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes Ans is 4 due to address arithmatic (P1-p2)/sizeof(char) Shubham Pandey 2 answered Sep 10, 2016 Shubham Pandey 2 comment Share Follow See all 4 Comments See all 4 4 Comments reply cse23 commented Sep 10, 2016 reply Follow Share can u explain/................. 0 votes 0 votes Shubham Pandey 2 commented Sep 10, 2016 reply Follow Share let the Base Address of array be 100 P2=100 take int size 2 byte then P1=108 p1-p2=(108-100)/2 divide by 2 is used because of addrress arithmetic 0 votes 0 votes cse23 commented Sep 10, 2016 reply Follow Share i have doubt when p1 is incremented and points to 102 at that time only *p1is 0 which will make cndition as false then how p1 =108? 0 votes 0 votes Shubham Pandey 2 commented Sep 11, 2016 reply Follow Share yes, you are right i didnt notice it and my ans is work when int * is replaced with char * this program produce Error cannot convert "const char * " to " int * " in intialization 0 votes 0 votes Please log in or register to add a comment.