IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Const - Discussion

@ : Home > C Programming > Const > Find Output of Program - Discussion

Read more:

"No one is as deaf as the man who will not listen."
- (Proverb)
5. 

What will be the output of the program in TurboC?

#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10, j=20;
    const int *ptr = &i;
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;
}

[A]. i= FFE2 ptr=12 j=FFE4 ptr=24
[B]. i= FFE4 ptr=10 j=FFE2 ptr=20
[C]. i= FFE0 ptr=20 j=FFE1 ptr=30
[D]. Garbage value

Answer: Option D

Explanation:

No answer description available for this question.


Prashant Dixit said: (Sun, Jan 30, 2011 03:21:07 AM)    
 
There must be an error in this program because ptr is declared as of constant type and it can not be modified so value of j can not be stored in ptr.

Himanshu said: (Mon, Feb 21, 2011 10:17:35 AM)    
 
Can't get it. Please explain it in step by step procedure.

Sundar said: (Fri, Mar 18, 2011 12:15:37 PM)    
 
Output:

In Turbo C (under DOS - 16 bit platform)

i = FFF4 ptr = 10 j = FFF2 ptr = 20

In GCC (under Linux - 32 bit platform)

i = BF93368C ptr = 10 j = BF933688 ptr = 20

I hope this may be help you a bit. Have a nice day guys!

Ajay said: (Sat, Jul 9, 2011 02:11:26 PM)    
 
const int *ptr = &i;

It declares a pointer which points to integer of constant type.
Pointer is not constant.So we can change it.

Sharath said: (Thu, Jul 14, 2011 09:50:27 PM)    
 
We can change the value of constant pointer

Sharath said: (Thu, Jul 14, 2011 10:02:19 PM)    
 
int i=10;
const int *ptr=10; //correct
*ptr=&i; //correct
*ptr=10; //wrong

Siva Pavan said: (Wed, Sep 14, 2011 10:58:29 PM)    
 
Coming to 2nd printf statement, since in pointers &p represents address,*p represents value then clearly p is declared as pointer preccedding by const keyword so its value is always constant since it is addressed to x(p=&x) the only possible value for p is x value i.e 10.

Karun Das said: (Thu, Feb 16, 2012 09:21:52 AM)    
 
Yes, here an object is a constant, we can't change pointer here,

Totally confusion, please correct me if I am thinking in incorrect way.

Rupinderjit said: (Mon, May 21, 2012 10:09:02 PM)    
 
Here object is constant not pointer pointed to by it. So ptr can point to any other address unless we declare int const*ptr=&i;, here pointer points to particular location is constant not an object.

Ashok said: (Fri, Oct 5, 2012 11:37:21 AM)    
 
"const int *ptr" means ptr is a pointer to const integer so we can modify ptr value.

Where as "int const *ptr" is different one it means ptr is const pointer to int whose value could not be changed. We have to define it at the declaration of that pointer itself.

Vineet Yadav said: (Mon, Mar 18, 2013 10:02:29 PM)    
 
const int *ptr;
int const* ptr;

It means pointer points to const integer value. that means we cant change the value of that integer but pointer can change its pointing location.

int * const ptr;
const * int ptr;

It means that pointer is constant here, it can point to an integer and then it cant change its pointing location.

Chaz said: (Sat, Apr 13, 2013 07:53:04 PM)    
 
Simplifying the above explanation,

const int *ptr //cannot modify *ptr

int* const ptr //cannot modify ptr


Difference in ptr and *ptr is self-explanatory.

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!