The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+3 votes
159 views

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.

asked in Programming by Active (2.1k points)
edited by | 159 views
0

See Arjun sir's comment here https://gateoverflow.in/164145/programming

1 Answer

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

answered by Loyal (8.9k points)
0
Brother just select the answer if you liked it and No thanks please
0

output will be -20, correct ur ans

https://ideone.com/GOpLzR

0
I don't know why it's printing 20. I use ubuntu OS and atom editor and in my terminal it is printing -3
+1
Why output is -20.

It should be -3.
0
@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
@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
but how -3?
0
@srestha,

see answer by Shivam, he has explained well..
0
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?
+1

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

0
@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
cleared.Thanks for the explanation .
0

yes thank u @ joshi_nitish

0

@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
yes, this is not a good question. The only pointer type in C which can point to other types are void and char.


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

40,958 questions
47,599 answers
146,673 comments
62,335 users