dynamic memory allocating error
Hi!
I'm trying to make an implementation of the datatype RATIONAL in ADT way. The probleme is when I try to allocate memory for a new struct that I create in main.c.
I send the adress of the struct to rational_create, where I try to allocate memory to it and after that set values to the integers in the struct, but the way I try to allocate doesn't work.
the problem is how to allocate the memory in rational_create(rational *r, int n, int t).
The code looks like this:
main.c
===========
int main(void)
{
Rational a;
rational_create(Rational a, 2, 2);
system("PAUSE");
return 0;
}
rational.c
===========
#include <stdio.h>
#include <stdlib.h>
#include "rational.h"
struct rat_
{
int a1, b1;
}
void rational_create(Rational *r, int n, int t)
{
*r = (Rational)malloc(sizeof(struct rat_));
r->a1=n;
r->b1=t;
}
rational.h
===========
#include <stdio.h>
#include <stdlib.h>
struct rat_;
typedef struct rat_ Rational;
/*Allocate memory*/
void rational_create(Rational *r, int n, int t);
int gcd(int n, int t);
Rational add(Rational r);
Rational sub(Rational r);
and s on...