Why does pointer to an address of a double variable not work in C? [duplicate] The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experiencec-behaviour of printf(“%d”, <double>)How to nicely format floating numbers to String without unnecessary decimal 0?How do function pointers in C work?Why does printf not flush after the call unless a newline is in the format string?Why is address zero used for the null pointer?printf by given pointer and format string. Issue with floatsHow do I achieve the theoretical maximum of 4 FLOPs per cycle?Why does Math.round(0.49999999999999994) return 1?Why does the C preprocessor interpret the word “linux” as the constant “1”?Memory addressing and pointersWhy can't I assign the address of a variable of one type(say double) to a pointer of int type?
Is this wall load bearing? Blueprints and photos attached
What are these Gizmos at Izaña Atmospheric Research Center in Spain?
How to split my screen on my Macbook Air?
Did God make two great lights or did He make the great light two?
How to delete random line from file using Unix command?
Didn't get enough time to take a Coding Test - what to do now?
Do working physicists consider Newtonian mechanics to be "falsified"?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Who or what is the being for whom Being is a question for Heidegger?
Why is Captain Marvel translated as male in Portugal?
Single author papers against my advisor's will?
Why is superheterodyning better than direct conversion?
How do you keep chess fun when your opponent constantly beats you?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
I'm thinking of a number
Make it rain characters
Can a novice safely splice in wire to lengthen 5V charging cable?
I could not break this equation. Please help me
How are presidential pardons supposed to be used?
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Can smartphones with the same camera sensor have different image quality?
Wall plug outlet change
How should I replace vector<uint8_t>::const_iterator in an API?
Difference between "generating set" and free product?
Why does pointer to an address of a double variable not work in C? [duplicate]
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experiencec-behaviour of printf(“%d”, <double>)How to nicely format floating numbers to String without unnecessary decimal 0?How do function pointers in C work?Why does printf not flush after the call unless a newline is in the format string?Why is address zero used for the null pointer?printf by given pointer and format string. Issue with floatsHow do I achieve the theoretical maximum of 4 FLOPs per cycle?Why does Math.round(0.49999999999999994) return 1?Why does the C preprocessor interpret the word “linux” as the constant “1”?Memory addressing and pointersWhy can't I assign the address of a variable of one type(say double) to a pointer of int type?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question is an exact duplicate of:
c-behaviour of printf(“%d”, <double>) [duplicate]
1 answer
following the example : (working with x64 cpu and x64 GCC)
int a=7; printf("%d",*&a); //It prints 7
double a=7; printf("%d",*&a); //It prints an address
edited: thanks, i got it, i have to use %lf, but since 7 is stored within 1 byte , now i just want to access that byte. so, how to point to that lower byte of the double variable?
c printf double
marked as duplicate by Antti Haapala
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 7:01
This question was marked as an exact duplicate of an existing question.
|
show 2 more comments
This question is an exact duplicate of:
c-behaviour of printf(“%d”, <double>) [duplicate]
1 answer
following the example : (working with x64 cpu and x64 GCC)
int a=7; printf("%d",*&a); //It prints 7
double a=7; printf("%d",*&a); //It prints an address
edited: thanks, i got it, i have to use %lf, but since 7 is stored within 1 byte , now i just want to access that byte. so, how to point to that lower byte of the double variable?
c printf double
marked as duplicate by Antti Haapala
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 7:01
This question was marked as an exact duplicate of an existing question.
You don't assume that the result7
is the address ofa
, do you? The expression*&
takes an address and then dereferences it. Remove the*
to actually get the address.
– harper
Mar 22 at 6:50
1
Use%lf
to printdouble
variable not%d
. And then this*&a
doesn't print address as both*
and&
nullify each other and it prints7.000000
.
– Achal
Mar 22 at 6:56
Removing the'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage forint
.
– David C. Rankin
Mar 22 at 6:57
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
2
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24
|
show 2 more comments
This question is an exact duplicate of:
c-behaviour of printf(“%d”, <double>) [duplicate]
1 answer
following the example : (working with x64 cpu and x64 GCC)
int a=7; printf("%d",*&a); //It prints 7
double a=7; printf("%d",*&a); //It prints an address
edited: thanks, i got it, i have to use %lf, but since 7 is stored within 1 byte , now i just want to access that byte. so, how to point to that lower byte of the double variable?
c printf double
This question is an exact duplicate of:
c-behaviour of printf(“%d”, <double>) [duplicate]
1 answer
following the example : (working with x64 cpu and x64 GCC)
int a=7; printf("%d",*&a); //It prints 7
double a=7; printf("%d",*&a); //It prints an address
edited: thanks, i got it, i have to use %lf, but since 7 is stored within 1 byte , now i just want to access that byte. so, how to point to that lower byte of the double variable?
This question is an exact duplicate of:
c-behaviour of printf(“%d”, <double>) [duplicate]
1 answer
c printf double
c printf double
edited Mar 22 at 7:10
bipul kalita
asked Mar 22 at 6:48
bipul kalitabipul kalita
197
197
marked as duplicate by Antti Haapala
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 7:01
This question was marked as an exact duplicate of an existing question.
marked as duplicate by Antti Haapala
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 7:01
This question was marked as an exact duplicate of an existing question.
You don't assume that the result7
is the address ofa
, do you? The expression*&
takes an address and then dereferences it. Remove the*
to actually get the address.
– harper
Mar 22 at 6:50
1
Use%lf
to printdouble
variable not%d
. And then this*&a
doesn't print address as both*
and&
nullify each other and it prints7.000000
.
– Achal
Mar 22 at 6:56
Removing the'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage forint
.
– David C. Rankin
Mar 22 at 6:57
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
2
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24
|
show 2 more comments
You don't assume that the result7
is the address ofa
, do you? The expression*&
takes an address and then dereferences it. Remove the*
to actually get the address.
– harper
Mar 22 at 6:50
1
Use%lf
to printdouble
variable not%d
. And then this*&a
doesn't print address as both*
and&
nullify each other and it prints7.000000
.
– Achal
Mar 22 at 6:56
Removing the'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage forint
.
– David C. Rankin
Mar 22 at 6:57
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
2
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24
You don't assume that the result
7
is the address of a
, do you? The expression *&
takes an address and then dereferences it. Remove the *
to actually get the address.– harper
Mar 22 at 6:50
You don't assume that the result
7
is the address of a
, do you? The expression *&
takes an address and then dereferences it. Remove the *
to actually get the address.– harper
Mar 22 at 6:50
1
1
Use
%lf
to print double
variable not %d
. And then this *&a
doesn't print address as both *
and &
nullify each other and it prints 7.000000
.– Achal
Mar 22 at 6:56
Use
%lf
to print double
variable not %d
. And then this *&a
doesn't print address as both *
and &
nullify each other and it prints 7.000000
.– Achal
Mar 22 at 6:56
Removing the
'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage for int
.– David C. Rankin
Mar 22 at 6:57
Removing the
'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage for int
.– David C. Rankin
Mar 22 at 6:57
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
2
2
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24
|
show 2 more comments
3 Answers
3
active
oldest
votes
You don't print a double
with %d
format specifier. You must use %f
.
Using wrong format specifier invokes undefined behaviour. The output (if you get one), cannot be justified in any way.
add a comment |
Because you are printing double with "%d
". Change it to "%lf
"
Regarding "//It prints an address"
It is not printing address. Printf is taking address of double variable and dereferencing it as an integer. So whatever value it is printing is some junk number.
2
for printf,%lf
is a myth. :)
– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
Actually @MayurK, Afloat
when passed to aprintf
, will be promoted todouble
beforeprintf
receives it. hence it works with both%f
and%lf
.
– Rai
Mar 22 at 8:57
add a comment |
"Why does pointer to an address of a double variable not work in C?"
No. It works.
double a=7; printf("%d",*&a); //It prints an address
- It's just printing the data at the address specified by
&a
. The*
is for dereferencing the address.
And if you are saying that it's not working because it isn't printing the value of double a
, it's simply because you are not using the right format specifier.
%d
is the format specifier forint
. If you want to print adouble
use%lf
or%f
.double a=7; printf("%lf",*&a); //This will print the double value
No. It works.
..that's an overstatement.
– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't print a double
with %d
format specifier. You must use %f
.
Using wrong format specifier invokes undefined behaviour. The output (if you get one), cannot be justified in any way.
add a comment |
You don't print a double
with %d
format specifier. You must use %f
.
Using wrong format specifier invokes undefined behaviour. The output (if you get one), cannot be justified in any way.
add a comment |
You don't print a double
with %d
format specifier. You must use %f
.
Using wrong format specifier invokes undefined behaviour. The output (if you get one), cannot be justified in any way.
You don't print a double
with %d
format specifier. You must use %f
.
Using wrong format specifier invokes undefined behaviour. The output (if you get one), cannot be justified in any way.
answered Mar 22 at 6:52
Sourav GhoshSourav Ghosh
111k15132191
111k15132191
add a comment |
add a comment |
Because you are printing double with "%d
". Change it to "%lf
"
Regarding "//It prints an address"
It is not printing address. Printf is taking address of double variable and dereferencing it as an integer. So whatever value it is printing is some junk number.
2
for printf,%lf
is a myth. :)
– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
Actually @MayurK, Afloat
when passed to aprintf
, will be promoted todouble
beforeprintf
receives it. hence it works with both%f
and%lf
.
– Rai
Mar 22 at 8:57
add a comment |
Because you are printing double with "%d
". Change it to "%lf
"
Regarding "//It prints an address"
It is not printing address. Printf is taking address of double variable and dereferencing it as an integer. So whatever value it is printing is some junk number.
2
for printf,%lf
is a myth. :)
– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
Actually @MayurK, Afloat
when passed to aprintf
, will be promoted todouble
beforeprintf
receives it. hence it works with both%f
and%lf
.
– Rai
Mar 22 at 8:57
add a comment |
Because you are printing double with "%d
". Change it to "%lf
"
Regarding "//It prints an address"
It is not printing address. Printf is taking address of double variable and dereferencing it as an integer. So whatever value it is printing is some junk number.
Because you are printing double with "%d
". Change it to "%lf
"
Regarding "//It prints an address"
It is not printing address. Printf is taking address of double variable and dereferencing it as an integer. So whatever value it is printing is some junk number.
answered Mar 22 at 6:57
MayurKMayurK
1,441619
1,441619
2
for printf,%lf
is a myth. :)
– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
Actually @MayurK, Afloat
when passed to aprintf
, will be promoted todouble
beforeprintf
receives it. hence it works with both%f
and%lf
.
– Rai
Mar 22 at 8:57
add a comment |
2
for printf,%lf
is a myth. :)
– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
Actually @MayurK, Afloat
when passed to aprintf
, will be promoted todouble
beforeprintf
receives it. hence it works with both%f
and%lf
.
– Rai
Mar 22 at 8:57
2
2
for printf,
%lf
is a myth. :)– Sourav Ghosh
Mar 22 at 7:04
for printf,
%lf
is a myth. :)– Sourav Ghosh
Mar 22 at 7:04
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
@SouravGhosh Just for my understanding: Then why/when "%lf" is required? How printf() differentiates float and double?
– MayurK
Mar 22 at 8:52
1
1
Actually @MayurK, A
float
when passed to a printf
, will be promoted to double
before printf
receives it. hence it works with both %f
and %lf
.– Rai
Mar 22 at 8:57
Actually @MayurK, A
float
when passed to a printf
, will be promoted to double
before printf
receives it. hence it works with both %f
and %lf
.– Rai
Mar 22 at 8:57
add a comment |
"Why does pointer to an address of a double variable not work in C?"
No. It works.
double a=7; printf("%d",*&a); //It prints an address
- It's just printing the data at the address specified by
&a
. The*
is for dereferencing the address.
And if you are saying that it's not working because it isn't printing the value of double a
, it's simply because you are not using the right format specifier.
%d
is the format specifier forint
. If you want to print adouble
use%lf
or%f
.double a=7; printf("%lf",*&a); //This will print the double value
No. It works.
..that's an overstatement.
– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
add a comment |
"Why does pointer to an address of a double variable not work in C?"
No. It works.
double a=7; printf("%d",*&a); //It prints an address
- It's just printing the data at the address specified by
&a
. The*
is for dereferencing the address.
And if you are saying that it's not working because it isn't printing the value of double a
, it's simply because you are not using the right format specifier.
%d
is the format specifier forint
. If you want to print adouble
use%lf
or%f
.double a=7; printf("%lf",*&a); //This will print the double value
No. It works.
..that's an overstatement.
– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
add a comment |
"Why does pointer to an address of a double variable not work in C?"
No. It works.
double a=7; printf("%d",*&a); //It prints an address
- It's just printing the data at the address specified by
&a
. The*
is for dereferencing the address.
And if you are saying that it's not working because it isn't printing the value of double a
, it's simply because you are not using the right format specifier.
%d
is the format specifier forint
. If you want to print adouble
use%lf
or%f
.double a=7; printf("%lf",*&a); //This will print the double value
"Why does pointer to an address of a double variable not work in C?"
No. It works.
double a=7; printf("%d",*&a); //It prints an address
- It's just printing the data at the address specified by
&a
. The*
is for dereferencing the address.
And if you are saying that it's not working because it isn't printing the value of double a
, it's simply because you are not using the right format specifier.
%d
is the format specifier forint
. If you want to print adouble
use%lf
or%f
.double a=7; printf("%lf",*&a); //This will print the double value
edited Mar 22 at 8:51
answered Mar 22 at 6:59
RaiRai
1,1424823
1,1424823
No. It works.
..that's an overstatement.
– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
add a comment |
No. It works.
..that's an overstatement.
– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
No. It works.
..that's an overstatement.– Sourav Ghosh
Mar 22 at 7:04
No. It works.
..that's an overstatement.– Sourav Ghosh
Mar 22 at 7:04
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
Sorry @SouravGhosh, I didn't quite get you.
– Rai
Mar 22 at 8:52
add a comment |
You don't assume that the result
7
is the address ofa
, do you? The expression*&
takes an address and then dereferences it. Remove the*
to actually get the address.– harper
Mar 22 at 6:50
1
Use
%lf
to printdouble
variable not%d
. And then this*&a
doesn't print address as both*
and&
nullify each other and it prints7.000000
.– Achal
Mar 22 at 6:56
Removing the
'*'
will result in undefined behavior on x64 as typesize for a pointer exceeds storage forint
.– David C. Rankin
Mar 22 at 6:57
Your edit made the question worse. No, there is no "byte with 7"
– Antti Haapala
Mar 22 at 7:20
2
@bipulkalita I repeat. There is no byte with value 7, no matter how much you try to argue the case. So there is no way you're going to find a byte with value seven, period. Please google for IEEE 754
– Antti Haapala
Mar 22 at 7:24