Ah, thanks! Sorry about being an ass earlier. >.>;;
EDIT: Well, now it's compiling, but it's not saving the strings into seperate structs. Anyone have any idea why?
Ah, thanks! Sorry about being an ass earlier. >.>;;
EDIT: Well, now it's compiling, but it's not saving the strings into seperate structs. Anyone have any idea why?
Last edited by Graham Aker; 01-25-2009 at 05:24 PM.
Can you post your working code that compiled?
OS: Linux Mint 13(Maya) LTS 64 bit.
Sure. Here it is:
Code:#include <stdlib.h> #include <stdio.h> #define big 2000 #define length 20 struct integer { int* digits; int size; }; int i, j, k, stringnum; int readnum(int stringnum, char* numbr, struct integer *emp); int main(){ FILE *ifp; char numbr[big]; struct integer *num[length]; //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, "%s", numbr); readnum(k, numbr, num[k]); } //Read the array printf("\n The arrays are: "); for (k=0; k<stringnum; k++){ for (i=0; i<j; i++) printf ("%d ", num[k]->digits[i]); printf("\n"); } system("PAUSE"); return 0; }; //Define functions int readnum(int stringnum, char* numbr, struct integer *emp){ int arraysize = strlen(numbr); emp->digits = malloc(sizeof(*emp->digits) * arraysize); //Move the integer into the array for(i=arraysize, j=0; i>-1; i--){ if(isdigit(numbr[i])){ emp->digits[j++] = numbr[i] - '0'; } } emp->size = j; };
When I compile that code, and try to run the program, it crashes. Does it run at all for you?
OS: Linux Mint 13(Maya) LTS 64 bit.
It was running... odd, I'll have to check out what's going on...
EDIT: Huh, it's not running anymore... Try running it in Debug mode.
EDIT 2: It works fine in debug, except the first line gets the first digit chopped off and it ends in yet another Access Violation. any ideas on what's causing this one?
Last edited by Graham Aker; 01-25-2009 at 06:03 PM.
This gives you an array of pointers to integer objects, that would point to integer objects if you had any (which you don't). It does not give you any integer objects. Since you want integer objects, you need to not declare an array of pointers, but an array of objects. (This also causes other errors, where you have e.g. num[k]-> when you would really need num[k]. instead. Note that even when you get dynamic array-ness working, you would still need num[k]. instead of num[k]->.Code:struct integer *num[length];
I've noticed some strange things with the code
Why did you declare digits as a pointer?Code:struct integer { int* digits; <===? int size; };
digits is never initialised(never had any value assigned to it), so it contains garbage. Then when you try to use it here
printf is trying to print out garbage. And you are treating digits[i] as an integer(i think that's what you want to do), when you declared it as a pointer.Code:printf ("%d ", num[k]->digits[i]);
I don't know, I am a newbie myself, so I could be wrong.
Last edited by happyclown; 01-25-2009 at 06:20 PM.
OS: Linux Mint 13(Maya) LTS 64 bit.
I know it's hard, but you must refrain from typing *.
What does it mean? It looks pretty straightforward to me: don't type *. Remember the line we're talking about, notice how it has a * in it, and don't do that.
I think tabstop means to stop using an array of pointers, and to just use a normal array.
OS: Linux Mint 13(Maya) LTS 64 bit.
Part of the problem with that line is that the professor specifically said it had to be like that. I even posted the excerpt from the assignment instructions.
So in other words, I need to find a way around that line. Any ideas?
Which line? int *digits is fine. struct integer *num[length] is most assuredly not.