2012-01-11 2 views
3

float var가 들어있는 구조체가 있습니다. 구조체에 대한 포인터를 사용하여 값을 읽으려고합니다.포인터를 사용하여 구조체 값을 읽는 중

struct mas { 
    float m; 
}; 

int main(void) 
{ 
    struct mas *ms; 
    ms=(struct mas*)malloc(sizeof(struct mas)); 
    scanf("%f",&(ms->m)); 
    printf("%f",ms->m); 
    return 0; 
} 

을하지만, 프로그램을 실행하면 다음과 같은 오류가 발생합니다 : 여기 코드는 사용

scanf floating point formats not linked 

컴파일러는 볼랜드 터보 C입니다 ++ (3.0) Windows PC에서. 이게 왜 그렇게?

+3

'malloc'의 반환 값을 전송할 필요가 없으며 권장되지 않습니다. –

+0

Visual Studio Express와 같은 최신 컴파일러 (TC++ 3는 20 살)를 사용하는 것이 더 좋을 것이라고 생각합니다. –

+0

이 StackOverflow의 질문에 대한 답변을 참조하십시오 http://stackoverflow.com/questions/6223453/how-to-enable-linking-floating-point-library-in-turboc –

답변

5

이 도움이 될 수 있습니다 또한

Borland's compilers try to be smart and not link in the floating- point (f-p) library unless you need it. Alas, they all get the decision wrong. ... (To force them to link it) define this function somewhere in a source file but don't call it:

static void forcefloat(float *p) 
{ 
    float f = *p; 
    forcefloat(&f); 
} 

:

If you have Borland C++ 3.0, the README file documents a slightly less ugly work-around. Insert these statements in your program:

extern unsigned _floatconvert; 
#pragma extref _floatconvert 
+0

은 이전 MS-DOS 시절에 차이점을 만들었습니다. 640KB의 메모리 제한이 있지만 요즘에는 ... –

1

내가 컴파일에서이 코드를 실행할 수 있어요 문서에서 http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-5.html

GCC 4.2.1 :

#include <stdlib.h> 
#include <stdio.h> 

struct mas{ 
    float m; 
}; 

int main() 
{ 
    struct mas *ms; 
    ms=malloc(sizeof(struct mas)); 
    scanf("%f",&(ms->m)); 
    printf("%f\n",ms->m); 
    return 0; 
} 

#include 문이 누락 되었습니까? 결과를 malloc()에서 전송해야한다고 생각하지 않습니다.

3

Why is this so..

고대의 쓸모없는 컴파일러에 a bug이 있기 때문에. 부동 소수점 연산을 올바르게 처리하는 최신 버전으로 업그레이드하십시오.

GCC의 최신 버전을 선택하거나 Microsoft의 Visual C++ Express 패키지를 무료로 다운로드 할 수 있습니다.이 패키지는 세계적인 수준의 IDE와 함께 컴파일러를 번들로 제공합니다.

+0

글쎄, 좋은 제안을 설명했다. – Fletch

관련 문제