Click here to Skip to main content
14,922,577 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i wrote the following program to separate the real and fractional parts of a number, where i declared a pointer to a structure. The program compiles perfectly, but does not execute.
I compiled using the Visual Studio command prompt. The part that causes trouble is where I have declared a pointer to the structure.
#define MAX 12
struct REAL
    int left;
    int right;
} *rl;

int main()
    //rl = (struct REAL *)malloc(sizeof(struct REAL));
    int l=0, r=0;
    int number[MAX];
    int ch, count = 0, sign=1;
    while((ch = getchar()) != '.')
        if(ch == '-')
            sign = -1;
            l = l*10 + (ch - '0');
    rl->left = sign*l;
    printf("left = %d\n", rl->left);
    while((ch = getchar()) != '\n')
        r = r*10 + (ch - '0');
    rl->right = r;
    printf("right = %d\n", rl->right);
    return 0;

Please point out the error!
thanx in advance.

(P.S. I am aware of the implementation of a real number in C, using the sign, exponent and fraction form(32 = 1+8+23).
I am learning some new data structure concepts. This program was a solution for a question in the book by Aaron M. Tenenbaum.)
Updated 7-May-12 16:24pm
Reason for my vote of 1
Should I also mark it off-topic? Here is why: this is a site for software developers and students; and software developers never say "it does not work", only lame users do. The software developers, even the beginners, say what exactly goes wrong, in what line of code. They provide comprehensive issue report.

In your case, you could run it under debugger to see what's wrong and ask a question if you still cannot understand the behavior or don't see how to fix it.

Use "Improve question" above.

wizardzz 7-May-12 13:28pm
Reason for my vote of 1
What is the actual problem you are having?
By the way, why separate fractional and integer parts of a number, ever?

kr_harsha wrote:
Please point out the error!

Oh the irony: the error is the pointer!
You didn't allocate a struct for it.

A quick & dirty fix (the code is far from being perfect): add the following lines immediately before the first while loop:
struct REAL real;
rl = ℜ 

and the statement r=0; immediately before the second while loop.
Sure, a 5.
Among other things: you cannot really make a satisfactory model of a real number using two integers. The best known model is the floating-point model which is based on significand, coefficient, mantissa, and exponent and already exists and standardized as IEEE 754:[^],[^].

This is a binary representation; and you are trying to work with strings instead of binary objects, which is itself is a pointless activity — working with string representation of data instead of data itself.

By the way, do you understand that a mathematical real number, its single instance, in general case, carries infinite amount of information?

I would advice you to stop doing what you are doing and learn some mathematics and elementary computer science around these problems.

JackDingler 7-May-12 14:06pm
If your number range is finite, fixed decimal binary works well.. Haven't needed it since 486s went out of style... Floating point is too fast now, to make optimizations like that worthwhile.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900