Thread: Access Violation

  1. #61
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Which line? int *digits is fine. struct integer *num[length] is most assuredly not.
    Oh, that one. Yeah, I can take the * out there. >.>;;

  2. #62
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Graham Aker, can you post the contents of the file that you are trying to read?

    EDIT: if you haven't already done so in this long thread.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  3. #63
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Aright, I removed the *, and I got an error on line 39 about an incompatable type in argument 3 of 'readnum'. How would I fix this?

    Oh, yeah, the code:
    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("The arrays are: \n");
          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;
    };
    EDIT: Durr, the input file. Here THAT is:
    Code:
    2
    31843694157942314379137864165
    2986783975917945029875942976

  4. #64
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pass the address of the specific thing you want to read to the function.

  5. #65
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by Graham Aker View Post

    //Scan for the number of strings
    fscanf(ifp, "%d", &stringnum);

    This doesn't look right.

    Code:
    int stringnum;
    If the file had this number:

    123456

    Code:
    fscanf(ifp, "%d", &stringnum);
    Would result in stringnum = 123456;

    If your intention is to determine how many strings(lines) there are in the file, use fgets, which gets a string(line) at a time, then use a counter to count how many strings were read(using a loop).
    OS: Linux Mint 13(Maya) LTS 64 bit.

  6. #66
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by happyclown View Post
    This doesn't look right.

    Code:
    int stringnum;
    The first line of the file has the number of numbers in it.

  7. #67
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Pass the address of the specific thing you want to read to the function.
    ...Uh, what? That's a bit vague...

    Oh, and joy, I just found what his input file was. Posting that now...

    Code:
    3
    1 8888888888 2222222222
    2 9999999999 10000000000
    2 10000000000 9999999999
    In those three bottom lines, I'm guessing the first number is the option he wants run (1 for addition, 2 for subtraction, and 3 for comparison, based off the sample output) and the following numbers are the two strings. This actually saves me a bit of trouble along the lines of storing the data in a structure array since it's different for each option and it just prints right there, but it still means I screwed up bad and need to rewrite a decent amount of the code...

    And he even gave me the funtions he wanted to use, too:
    Code:
    //Preconditions: the first parameter is string that stores
    //               only contains digits, doesn't start with
    //               0, and is 200 or fewer characters long.
    //Postconditions: The function will read the digits of the
    //	large integer character by character, 
    //	convert them into integers and return a 
    //             pointer to the appropriate struct integer.
    struct integer* read_integer(char* stringInt);
    
    //Preconditions: p is a pointer to a big integer.
    //Postconditions: The big integer pointed to by p is 
    //                printed out.
    void print(struct integer *p);
    
    //Preconditions: p and q are pointers to struct integers.
    //Postconditions: A new struct integer is created that 
    //                stores the sum of the integers pointed to 
    //                by p and q and a pointer to it is 
    //                returned.
    struct integer* add(struct integer *p, struct integer *q);
    
    //Preconditions: p and q are pointers to struct integers.
    //Postconditions: A new struct integer is created that 
    //                stores the absolute value of the 
    //                difference between the two and a pointer 
    //                to this is returned.
    struct integer* subtract(struct integer *p, struct integer *q);
    
    //Preconditions: Both parameters of the function are 
    //	  pointers to struct integer. 
    //Postconditions: The function compares the digits of two 
    //	numbers and returns: 
    //    -1 if the first number is smaller than the second, 
    //     0 if the first number is equal to the second number,
    //   1 if the first number is greater than the second.  
    int compare(struct integer *p, struct integer *q);
    ...Yeah, I've got a lot of work to do. This is what happens when you don't read ALL of the instructions... >_<

  8. #68
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Graham Aker View Post
    ...Uh, what? That's a bit vague...
    No that is actually remarkably explicit. You pass the address of the specific object (that you want to store the number in) to the function.

  9. #69
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Oh, durr. Now I feel like a complete idiot. >_<;;

  10. #70
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by Graham Aker View Post
    Oh, durr. Now I feel like a complete idiot. >_<;;
    Don't worry about it.

    Have you got a headache yet? Maybe you should take a short break from it all.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  11. #71
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Okay, so does anyone have any idea how I can take a string and turn it into an integer array with a function that's header is "struct integer* read_integer(char* stringInt);"?

  12. #72
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by happyclown View Post
    Don't worry about it.

    Have you got a headache yet? Maybe you should take a short break from it all.
    I wish I could, honestly, but it's due tomorrow at midnight and I've got a three-part Java project due wednesday at midnight as well.

    Here's hoping the Java project's easier than this. o_o;;

  13. #73
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Graham Aker View Post
    Okay, so does anyone have any idea how I can take a string and turn it into an integer array with a function that's header is "struct integer* read_integer(char* stringInt);"?
    Given that you've had that all along, I would say "you". (You just have to return the integer object, or rather its address, instead of passing it in.)

  14. #74
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Given that you've had that all along, I would say "you". (You just have to return the integer object, or rather its address, instead of passing it in.)
    Seriously? That's it?
    ...Please excuse me while I bang my head against this desk here.

    ...No, wait, hold that thought, it's crashing when I try it. Though I'm sure I'm doing it horribly wrong...

    here's what I tried. Feel free to tell me how much I screwed it up:
    Code:
    struct integer* read_integer(char* stringInt){
          int arraysize = strlen(stringInt);      
          int intarray[big];
          
    //Move the integer into the array      
          for(i=arraysize, j=0; i>-1; i--){
               if(isdigit(stringInt[i])){
                    intarray[i]=stringInt[i] - '0';
               }
          }
          return intarray[i];
    };

  15. #75
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    horribble code
    what you store in your intarray if the char is not a digit?
    you return value of the int that is not initialized
    you loose all you work - because the inarray is a local var that is detroyed after the function ends
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

Tags for this Thread