thomashw Posted October 18, 2008 Share Posted October 18, 2008 Can someone explain how to use scanf with a structure? Here's the code I'm trying to use, and it's not working: #include<stdio.h> typedef struct hey{ double x; double y; } hey; int main() { hey whatsup; scanf( "%f", &whatsup.x ); printf( "%lf", whatsup.x ); } Whatever I enter, the printf function prints "0.000000". Quote Link to comment https://forums.phpfreaks.com/topic/129014-solved-c-regarding-structures-and-scanf/ Share on other sites More sharing options...
corbin Posted October 18, 2008 Share Posted October 18, 2008 It looks to me like one can't create a reference to a variable in a structure. (Maybe someone who knows C can tell us how?) Why not just do double tmp; scanf("%f", tmp); whatsup.x = tmp; Edit: Oh this is solved.... Hrmm never mind. Just of curiosity, how did ya do it? Quote Link to comment https://forums.phpfreaks.com/topic/129014-solved-c-regarding-structures-and-scanf/#findComment-668825 Share on other sites More sharing options...
thomashw Posted October 19, 2008 Author Share Posted October 19, 2008 scanf( "%f", &whatsup.x ); should have been: scanf( "%lf", &whatsup.x ); Doh. Quote Link to comment https://forums.phpfreaks.com/topic/129014-solved-c-regarding-structures-and-scanf/#findComment-668872 Share on other sites More sharing options...
corbin Posted October 19, 2008 Share Posted October 19, 2008 Ahhhhh.... Quote Link to comment https://forums.phpfreaks.com/topic/129014-solved-c-regarding-structures-and-scanf/#findComment-669465 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.