I'm a newfag to programming in C, and I'm having trouble with a function that I've written for a program that does operations with "big integers". Basically, my function runs once, perfectly, but on the second iteration, it crashes when it goes to allocate memory to the temporary structure.
I've tried a few things, but this one mostly has me stumped. Any help would be greatly appreciated, you don't have to tell me what to do but just a nudge in the right direction would be sufficient.
This is my function, complete with my debugging lines so you can get a better idea of what I've been doing:
struct integer* read_integer(char* stringInt){
int length = strlen(stringInt);
//COMMENCING DEBUGGING
printf("FUNCTION START\n");
printf("LENGTH VALUE = %d\n", length);
//DEBUGGING
printf("The SIZE value of the structure is currently: %d\n", temp->size);
//DEBUGGING
printf("stringInt currently contains all of the following:\n");
for(i=0;i<length;i++){
printf("%d: %c\n", i, stringInt[i]);
}
int i = 0;
int j = length;
while(stringInt[i]!='\0'){
temp->digits[j-1]=(int)stringInt[i];
temp->digits[j-1]-=48;
i++;
j--;
}
//MORE DEBUGGING
printf("The ARRAY within the STRUCT has the following VALUES:\n");
for(i=0;i<length;i++){
printf("%d: %d\n", i, *temp->digits);
}
//After DEREFERENCING the struct above by adding an asterisk, it displayed correct values. Function works so far.
//DEBUGGING
printf("FUNCTION COMPLETE\n\n\n");
return temp;
}
Name:
Anonymous2010-01-17 12:45
printf("%d: %d\n", i, *temp->digits);
Wouldn't this line just print out the same second number each time?
Also, telling us the parameters your passing into the function would probably help.
>>2
You're right, the reason I didn't catch that is because my test case was all the same number.
In this function, stringInt consists of 10 numbers as a string. The first case is 8888888888, the second one is 2222222222.
And yes, I made a thread earlier about a somewhat related problem, but I didn't think /prog/ was having bandwidth issues considering most of the threads here are only tangentially related to programming.
>>5
Up at the top you are mallocing an array of struct integers, but later on you are only using one struct integer and treating the pointer to an int digits as an array, but in reality you have only malloced a pointer to a single integer.
Try mallocing the struct first, then the integer array second and assigning the struct_integer->digits to that array.
This isn't C. This is C++ without class, I mean classes.
In C, we declare all of out variables before there is any other code, and variables are initialized to constant values only.
>>17
Show me your magical C90 standard. It's still compulsory in C89.
(If you meant ten years, nobody uses C99 because for the most part, it's just an attempt at legitimising bad habits that some influential compilers supported.)