The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+21 votes
2.6k views

Consider the following program in C language:

#include <stdio.h>

main()
{
   int i;
   int*pi = &i; 
   
   scanf("%d",pi);
   printf("%d\n", i+5);
}

Which one of the following statements is TRUE?

  1. Compilation fails.
  2. Execution results in a run-time error.
  3. On execution, the value printed is $5$ more than the address of variable $i$.
  4. On execution, the value printed is $5$ more than the integer value entered.
asked in Programming by Veteran (103k points)
edited by | 2.6k views
0
yes here memory assigned at int i;
0
Beautiful Question Indeed !!

3 Answers

+30 votes
Best answer
int i; //i is declared
int*pi = &i;  //pi is a pointer variable 
//and is assigned the address of i

scanf("%d",pi); //i is overwritten with the value 
//we provided because pi is pointing to i earlier

printf("%d\n", i+5) //it will print the value stored in i+5

input=3; output=8

Option D is answer.

answered by Boss (14.3k points)
selected by
+39
scanf just puts the read value in the address passed to it. Usually we give this address using '&' operator. Here, we use a pointer variable and do this. Result is the same.
0
So, option D is correct :)
0

Ans is not D 
tried running on codechef...

https://www.codechef.com/ide

0
why not D)?
+3

@Lokesh (D) is correct. You may see this

+1
Ok (D) is correct but i m confused here

suppose i is at memory location 100

I think *pi has address of i...i.e, 100

and in scanf this 100 will be overwritten and not value of i
0

 Lokesh Dafale even codechef gives same ans...D is true...

+6
@Lokesh

U sopposed memory location of i=100 ok

after int *pi=&i;// content of pointer p=100 .

Now

scanf("%d",&i);

&:- is address of operator

&i:-Reads as address of i which is equals to content of pi so we can write them interchangeably it wont effect the result.

Means &i is 100% same as writing only pi and here both returns address of i which is 100

so->>scanf("%d",&i); is same as scanf("%d",pi);

I can move one level more by taking int **ptr=&pi;

and write scanf("%d",*ptr); which is same as above 2 defination of scanf.
+7

one more thing

scanf("%d",____);

this ___ may represent any valid address and here scanf will read an integer variable(due to %d) from user (i.e 10) and make that value(10) as the content of address____

//so as per ur doubt this value 10 will not overwrite with address 100 itself but it will make as the content of the provided address 100...which indirectly happens i=10

Now this ___ address u may write in any allowable way in c be it using address of operator(&) or using pointer.

0

https://gateoverflow.in/user/Rajesh+Pradhan, in your 1 level up question- please explain how correct answer is coming. I'm attaching screenshot of my understanding.

+1
@swati in your image, &ptr = 3000 and ptr = 2000 and hence *ptr = value at 2000 = 1000, so value will be entered at address 1000
0
@Swati Rauniyar, your $scanf("\%d",*ptr)$ is wrong. The question has $scanf("\%d",ptr)$.
0
I don't understand when their is no memory allocated to a variable at declaration time how it is possible to allocate it's address in a pointer
+5 votes
main()
{
   int i;//here i stores GARBAGE VALUE
   int*pi = &i; //p stores address of i
   
   scanf("%d",pi);// think like that scanf("%d",&i); because pi=&i; so we give value store at i(value at                  address)
   printf("%d\n", i+5);//if we give n output n+5
}
answered by Active (3.5k points)
0
best answer
+1 vote
There is no problem in the program as pi points to a valid location. Also, in scanf() we pass address of a variable and pi is an address.

So answer is D
answered by Loyal (8.5k points)


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,770 questions
47,478 answers
145,652 comments
62,241 users