3 3 votes Consider the following code int main(){ char *str = "ABCDEF"; printf("%d",fun(str)); return 0; } int fun(int *p1){ int *p2 = p1; while(*++p1); return (p2-p1); } The output of given program is_________. Assume object of data type int occupies 2 bytes. Programming in C strings bad-question + – Rohit Gupta 8 2.5k views answer comment Share Follow Print See 1 comment 1 1 comment reply Sukannya commented Oct 31, 2017 reply Follow flag See Arjun sir's comment here https://gateoverflow.in/164145/programming 0 0 replyShare Please log in or register to add a comment.
0 0 votes First of all in line int fun(int *p) it should be int fun(int *p1) Edit done Now str is pointer to string constant which is typecasted to integer pointer in fun(int *p1) therefore p1 and p2 are both integer pointers. Line while(*++p1); incements p1 pointer 2 bytes everytime till we reach the null value = 0 and this loop terminates. p2-p1 = {addressof(p2) - address of(p1) } / 2 since integer occupies two bytes p2 - p1 = -3 // p2 is at lower address and p1 is at higher address -3 is returned and printed Shivam Chauhan answered Oct 8, 2017 Shivam Chauhan comment Share Follow See all 15 Comments 15 15 Comments reply Shivam Chauhan commented Oct 8, 2017 reply Follow flag Brother just select the answer if you liked it and No thanks please 0 0 replyShare srestha commented Oct 9, 2017 reply Follow flag output will be -20, correct ur ans https://ideone.com/GOpLzR 0 0 replyShare Shivam Chauhan commented Oct 9, 2017 reply Follow flag I don't know why it's printing 20. I use ubuntu OS and atom editor and in my terminal it is printing -3 0 0 replyShare Shubhanshu commented Oct 11, 2017 reply Follow flag Why output is -20. It should be -3. 1 1 replyShare Anjan commented Oct 29, 2017 reply Follow flag @shivam, But how did u get p2- p1 = -6 in the first place...? (without dividing size of data) P2 is pointing to A and P1 is pointing to '\0' 0 0 replyShare joshi_nitish commented Oct 29, 2017 reply Follow flag @srestha, in ideone it is printing -20 because, actually the sizeof int = 4 bytes, but in qsn it is given to use sizeof int as 2 bytes 0 0 replyShare srestha commented Oct 29, 2017 reply Follow flag but how -3? 0 0 replyShare joshi_nitish commented Oct 29, 2017 reply Follow flag @srestha, see answer by Shivam, he has explained well.. 0 0 replyShare srestha commented Oct 29, 2017 reply Follow flag Not getting point See p2 pointing F and p1 pointing A and also p2 value is always greater than p1 value And in between p2 and p1 5 elements are there Where am I wrong? 0 0 replyShare joshi_nitish commented Oct 29, 2017 reply Follow flag p2 is not pointing to 'F', it is pointing to 'A' and p1 is pointing to '/0' now, see difference of pointers gives number of hops b/w them. since int is of 2 bytes, incrementing int pointer(p2 or p1) by 1, increments them with 2 bytes.... now see p2-p1 will give number of hops b/w two of them which is = 3, but we are doing p2-p1(p2 is at lower address), so put negative sign finally p2-p1 = -3 1 1 replyShare Anjan commented Oct 29, 2017 reply Follow flag @joshi while(*++p1); --- loop breaks and current position of p1 is after F. P2 points to A. Number of elements between is 6 is it correct ? 0 0 replyShare Anjan commented Oct 29, 2017 reply Follow flag cleared.Thanks for the explanation . 0 0 replyShare srestha commented Oct 29, 2017 reply Follow flag yes thank u @ joshi_nitish 0 0 replyShare Rohit Gupta 8 commented Oct 31, 2017 reply Follow flag @Arjun Sir's comment in similar question. You should not use an integer pointer to point to a character array or else behaviour is undefined in C. This is because C language supports memory alignment for integers where any integer can be assumed to start from an address which is a multiple of sizeof int. Now I'm confused. 0 0 replyShare Arjun commented Oct 31, 2017 reply Follow flag yes, this is not a good question. The only pointer type in C which can point to other types are void and char. 0 0 replyShare Please log in or register to add a comment.