Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Problem:initiation for my struct
Closed Thread
Old 02-20-2006, 12:32 AM   #1 (permalink)
 
Newb Techie

Join Date: Feb 2006

Posts: 4

smithmay

Default Problem:initiation for my struct

The program as follows can run good under C++ envirement.Under C,the output is:"scanf:floating point formats not linked. Abnormal program termination."
I don't know where the mistake is.
#include<stdio.h>
typedef struct student
{float score;
}student;
main()
{student *a;
a=(student*)malloc(sizeof(student));
scanf("%f",&a->score);
printf("The score is:%f\n",a->score);
}
But after I have changed some of this program,it works.The other one goes here:
#include<stdio.h>
typedef struct student
{float score;
}student;
main()
{student b;
scanf("%f",&b.score);
printf("The score is:%f\n",b.score);
}
It works without any problems.Why?What's the differece between them?
smithmay is offline  
Old 02-20-2006, 01:18 PM   #2 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default

touche buddy!! i dont know if this is a coincidence or what! but i have a C program in which it gave me the same error and when i accepted the value in a different variable and then stored it in the structure using pointer it worked.. i dont know why this happened. Also if you take the datatype of score as integer and keep everything same then also it works perfectly!! so guys if u reply for it with a solution it will be for the two of us!! thanks !! c ya!!
simple is offline  
Old 02-22-2006, 12:10 AM   #3 (permalink)
 
Newb Techie

Join Date: Feb 2006

Posts: 4

smithmay

Default

Maybe it is a bug of Turbo C 2.0.But I really wish some professional programmer can give us the answer!Even my teachers can not explain it exactly!
smithmay is offline  
Old 02-24-2006, 10:19 PM   #4 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

first of all, use [code ] tag to circle your code to perserve formatting..

otherwise, can't see anything wrong, i used gcc to run it, with the exception of one warning as listed below, runs perfectly..

test.c:9: warning: incompatible implicit declaration of built-in function 'malloc'

here is the code i used:
Code:
#include <stdio.h>
typedef struct student
{
	float score;
}student;
int main(void)
{
	student *a;
	a=(student*)malloc(sizeof(student));
	scanf("%f",&a->score);
	printf("The score is:%f\n",a->score);
	
	student b;
	scanf("%f",&b.score);
	printf("The score is:%f\n",b.score);
	return(0);
}

__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On