...Okay... So how do I fix it?
Which of the several corrections is it that you do not understand?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, I'm sure it is different from your original question on the 6th page of the thread.
But instead of asking a wide-ranging "how do I fix that" as a reply to someone (vart in this case) suggesting several things that are wrong in your code, ask a more specific question about what you don't understand - unless you completely do not understand the statement, in which case perhaps you should ask "I don't understand what you mean, can you explain what XYZ means".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Oh. Well, yeah, I guess I should have been more specific...
How do I make sure the value I want is returned in that function?
Right, so the first question is what you actually intend to do - since you are storing data in a local array of integers, you return the last element of that integer array [actually element -1, since that is when the loop is terminated] - the return type, however, is a pointer to struct integer. I bet you have a warning of "making pointer from integer without a cast" or some such on the last line.
I haven't read enough of your code to know how you go from an integer array to a struct integer, but I suspect it involves yet another loop.
I think vart also implies that if the input is not a digit, it simply leaves whatever rubbish happens to be in the intarray variable there when the input value is not a digit. You may want to do something slightly different for that.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
After a great deal of help from some of my friends, I think I fixed most of the problem in that function. However, somehow, I've gotten myself another Access Violation. Here's the new code:
Where exactly am I going wrong here?Code:struct integer* read_integer(char* stringInt){ int arraysize = strlen(stringInt); struct integer *read; read = (struct integer*)malloc(sizeof(struct integer)); read->size = arraysize; read->digits = (int*)malloc(sizeof(int)*arraysize); //Move the integer into the array for(i=0, j=0; i<arraysize; i++){ read->digits[arraysize-1-i]=(int)stringInt[i] - '0'; } return read; };
I cannot read your mind to understand "what you want to return"
But mostly - when function returns a pointer - it should allocate memory with malloc and return pointer to this memory.
Alternative solution - pass a struct integer* as a parameter (allocated in the calling function using malloc or on stack), and work in this function with that parameter
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
How would I check the return value of malloc?
Also, if it can help uncover where the Access Violation is, I'll post the full code again (Minus the extra functions)
Code:#include <stdlib.h> #include <stdio.h> #define big 200 struct integer { int* digits; int size; }; struct integer* read_integer(char* stringInt); int main(){ FILE *ifp; char numbr1[big]; char numbr2[big]; struct integer *num1; struct integer *num2; int stringnum; int i; int j; int k; int comp; int intarray[big]; int option; //Read in the file ifp = fopen("bigint.txt", "r"); //Scan for the number of strings fscanf(ifp, "%d", &stringnum); //Read in the numbers for (k=0; k<stringnum; k++){ fscanf(ifp, "%d, %s, %s", option, numbr1, numbr2); num1 = read_integer(numbr1); num2 = read_integer(numbr2); } //Read the array printf("The arrays are: \n"); system("PAUSE"); return 0; }; //Define functions struct integer* read_integer(char* stringInt){ int i; struct integer *read; read = (struct integer*)malloc(sizeof(struct integer)); read->size = strlen(stringInt); read->digits = (int*)(malloc(read->size*sizeof(int))); //Move the integer into the array int x = read->size - 1; for(i = read->size - 1; i>0; i++){ read->digits[i]=(int)(stringInt[x] - '0'); x--; } return read; }
You would check if it is a null pointer (e.g., you would check the value of read). If so, you would need to decide how you want to handle it, e.g. clean up by freeing the memory that has already been dynamically allocated and terminating the program, or in some perhaps more graceful way.Originally Posted by Graham Aker
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
assuming read->size >= 1, this loop will go on for quite some time - like 2 billion iterations or so, if you don't stomp outside your allocated memory.Code:for(i = read->size - 1; i>0; i++){
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I slapped a printf in there to check the value and I got a screenful of junk. So, how exactly would I free up that memory that's causing all of this?
EDIT: Wait, nevermind, it was indeed that loop. Thanks.
Last edited by Graham Aker; 01-26-2009 at 11:34 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yeah, it was that one. I flopped the > to a < and it compiled perfectly. Thanks for that!
Now the problem is the addition, subtraction, and comparison functions that come next...