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

Consider the following code

int main(){
    char *str = "ABCDEF";
    return 0;
int fun(int *p1){
    int *p2 = 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.2k points)
edited by | 165 views

See Arjun sir's comment here

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 (9.1k points)
Brother just select the answer if you liked it and No thanks please

output will be -20, correct ur ans

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

It should be -3.
@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'

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
but how -3?

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

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

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 ?

cleared.Thanks for the explanation .

yes thank u @ joshi_nitish


@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.

yes, this is not a good question. The only pointer type in C which can point to other types are void and char.

Related questions

–2 votes
0 answers
asked Sep 21, 2017 in Programming by Sunil8860 (141 points) | 69 views
0 votes
1 answer
asked Dec 14, 2017 in Programming by VS Loyal (9.9k points) | 161 views

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

46,966 questions
51,292 answers
66,643 users