A NULL pointer changed after returned?

From: fix (fix_at_here.com)
Date: 02/29/04


Date: Sun, 29 Feb 2004 01:14:15 -0600

Hi all,
I am new to C and I just started to program for a homework.
I included my code down there.
It is a fraction "class". I declared the Fraction struct, tried to
create some invalid fraction, with denominator zero.
A Fraction is created by the newFraction() function, and which in turns
call normalize() function to simplify it, but before, the normalize()
function will do a check, if the denominator is zero, it will give a
error by returning a NULL.
Everything works fine for a normal fraction, e.g. 3/4, 5/4, but if I
give a "4/0" to the function, it is frustrating.
In the normalize function, I am sure it is set to NULL, but back to
newFraction, it changed back to 4/0. But if I give a fraction that could
  be simplified, such as 6/8, it is simpified and returned as 3/4 with
no problem.
Please help!
fix.

Fraction.c:
#include "Fraction.h"
void Case1c()
{
        printFraction(newFraction(0,4));printf("\n");
        printFraction(newFraction(4,0));printf("\n");
        printFraction(newFraction(0,0));printf("\n");
}

int main(void)
{
        printf("Case 1c:\n");Case1c();
        return 0;
}

Fraction.h:
#include <stdio.h>

typedef struct {
        int sign;
        int numerator;
        int denominator;
} Fraction;

int signOf(int number){
        if (number < 0)
                return -1;
        else
                return 1;
}

int gcd(int num1, int num2){
        if (num1 < num2)
                return gcd(num2, num1);
        else
                if ((num1 % num2) == 0)
                        return num2;
                else
                        return gcd(num2, num1 % num2);
}

void normalize(Fraction *fr){
        int factor;
        if (fr->denominator == 0) // Invalid function
        {
                fr = NULL; // set it to NULL and return

                printf("Set fr = NULL\n");
                if (fr!=NULL)
                        printf("Not null");
                else
                        printf("null");
                return;
        }
        // Take the signs form the numerator and denominator to the sign variable
        fr->sign *= signOf(fr->numerator);
        fr->sign *= signOf(fr->denominator);

        // Absolute the numerator and denominator
        fr->numerator *= signOf(fr->numerator);
        fr->denominator *= signOf(fr->denominator);

        if (fr->numerator == 0)
        { // If the numerator is 0, simplify it to 0/1
                fr->denominator = 1;
                fr->sign = 1;
        }
        else
        { // Find the gcd of the numerator and denominator, divide them by the gcd
                factor = gcd(fr->numerator, fr->denominator);
                fr->numerator /= factor;
                fr->denominator /= factor;
        }
}

Fraction newFraction(int num, int den)
{
        Fraction fr;
        Fraction *frp;

        fr.numerator = num;
        fr.denominator = den;
        fr.sign = 1;
        frp = &fr;
        normalize(frp);
        if (frp!=NULL)
                printf("Not null");
        else
                printf("null");

        return fr;
}
void printFraction(Fraction fr)
{
        if (&fr == NULL)
                printf("Invalid fraction");

        if (fr.sign == -1)
                printf("-");
        printf("%i/%i", fr.numerator, fr.denominator);
}



Relevant Pages

  • Re: A NULL pointer changed after returned?
    ... static void Case1c ... int main ... Fraction newFraction; ... /* Take the signs form the numerator and denominator to the sign variable ...
    (comp.lang.c)
  • Re: division by 7 efficiently
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... Therefore, our fraction is ... int divide_by_7 ...
    (comp.programming)
  • Re: If statement for time
    ... David Biddulph ... Times are a fraction so the INTis removing the fraction, ... The Int() rounds the time value to remove the date. ...
    (microsoft.public.excel.misc)
  • Re: If statement for time
    ... Times are a fraction so the INTis removing the fraction, ... In Perth, ... The Int() rounds the time value to remove the date. ...
    (microsoft.public.excel.misc)
  • Re: Floating point to integer casting
    ... float to int causes truncation of any fractional part. ... 23 fraction bits. ...
    (comp.lang.c)