C Programming - Const - Discussion

Discussion Forum : Const - Find Output of Program (Q.No. 5)
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;
}
i= FFE2 ptr=12 j=FFE4 ptr=24
i= FFE4 ptr=10 j=FFE2 ptr=20
i= FFE0 ptr=20 j=FFE1 ptr=30
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 2 of 3.

Suyog said:   9 years ago
Please, can anybody explain it detail?

Surbhi said:   1 decade ago
% 5x here x is for hexadecimal value of ptr that has assigned to I and 5 specifies the width of ptr at least 5 digits wide (space also included).

Debasisd16061 said:   1 decade ago
What is %5x?

Varinder said:   1 decade ago
How constant may change?

Prashant dixit said:   1 decade ago
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.

Akshay11 said:   1 decade ago
What does %5x means?

Ritesh lenka said:   1 decade ago
Here the int value that pointed by ptr is const. Not the ptr. When we declare int const *ptr, then ptr is consider as a constant.

ChaZ said:   1 decade ago
Simplifying the above explanation,

const int *ptr //cannot modify *ptr

int* const ptr //cannot modify ptr


Difference in ptr and *ptr is self-explanatory.

Vineet Yadav said:   1 decade ago
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.

Ashok said:   1 decade ago
"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.


Post your comments here:

Your comments will be displayed after verification.