After finally getting introduced to programming through Python, I decided to start coding using C. Right now I am on project euler, and I am trying to solve problem 3 using memory allocation to fit an array in. I am well aware that there are tons of easier ways to do this however I would like to know how to do it this way to expand my knowledge. Whenever I run this code, I get very strange numbers for some reason. Can someone tell me how to do this concept? It is not too much code, it might seem long because I like to make alot of space. I would like to add that when i use numbers like 45 or 50 it is fine. It starts acting up when using the number in the problem which is 600851475143.
Thank you in advance!
#include <stdio.h>
int newprim_fac(long long int n){
int index=0;
int i=2;
long int MAXSIZE=100;
int*W=(int*)calloc(1,MAXSIZE);
while(n!=1){
if(n%i==0){
while(n%i==0){
printf("\n\n %d", i);
n=n/i;
W[index]=i;
index++;
}
}
i++;
if(sizeof(W)>=MAXSIZE){
*W=(int*)realloc(W,2*MAXSIZE);
}
}
printf("\n\nsize of W is: %d\n", sizeof(W));
for(i=0;i<sizeof(W);i++){
printf("\n\n%d", W[i]);
}
free(W);
return 0;
}