-
malloc arithmetic??
I am tring to build a program that deals with large numbers of 600+ digits, I know that I could not store these in an integer or even an unsigned long long, so I tried to use malloc() with the following syntax:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int *a = malloc(2048);
if(a==NULL){exit(EXIT_FAILURE);}
int *b = malloc(2048);
if(b==NULL){exit(EXIT_FAILURE);}
int *c = malloc(2048);
if(c==NULL){exit(EXIT_FAILURE);}
scanf("%i", &a);
scanf("%i", &b);
c=a/b; //This is where it fails on compile
printf("%i /n", c);
free(a);
free(b);
free(c);
system("PAUSE");
return 0;
}
The 2048 bits are just a test value, as is the function of the program (just to find out if I can use arithmetic on malloc() variables).
The compiler complains of 'Invalid Operands to 'binary''
Any ideas on how to solve this would be very helpful, I am using Dev C++ with the C compiler function.
Thanks in advance!
Phil
-
You can't divide pointers because the result would not be meaningful. If you are intending to write code to do arbitrary precision arithmetic, then you'll have to implement it. Otherwise, I've just given you a term to Google.
-
Code:
printf("%i /n", c);
You probably meant
Code:
printf("%i \n", c);
This isn't going to work since a is already a pointer . . . .
-
dwks 1: That was a copying mistake, It is correct in the code.
dwks 2: I wondered about that but I have checked again just now and it does work!
Dave_Sinkula: Thanks, I put that term into google and have found the GNU Multiple Precision Arithmetic library, which looks to be exactly what I want!
Thanks Everyone!
Phil