Access Violation

This is a discussion on Access Violation within the C Programming forums, part of the General Programming Boards category; Aright, here it is. This time including the Print function, since it's what's causing me this trouble. Code: #include <stdlib.h> ...

  1. #31
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Aright, here it is. This time including the Print function, since it's what's causing me this trouble.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define big 2000
    
    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 = malloc(sizeof(*num));
    
    //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);
          }
                           
    //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;
    };
    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';
               }
          }
    };

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've only got one string. So you had better only print one string.

    Before you start the for loop, you had better figure out what j is.

  3. #33
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    I'm actually not quite sure anymore... originally, it was supposed to be used to flip the array around, but then I just decided to save it in reverse order to begin with, so I guess it became pointless.

    How would I set it up so there'd be multiple strings?

    EDIT: No, wait, nevermind, j still aids in flipping the array.

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In your print loop, j is 12093476102934875-34097850921834701928475-0238947-023498u5-23495u. That is to say, it's random garbage because you never assign it a value.

  5. #35
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    j is only used in the loops, at the beginning of which it is set to 0.

    Also, I'm not sure if this means anything, but there's actually a .txt file that goes with the program. Do you guys need it to help with anything?

    Also note that the TXT file I use probably won't have the same number strings as the one in the actual final product, since mine's just a test file.

    The contents of the file, if anyone cares:
    2
    7432968527294356297648975290
    2986783975917945029875942976
    The first line tells how many strings are in the file, then the subsequent lines are individual strings.

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay I missed that j is global and so changing it in the function means changing it in main too. Sorry.

    However, that does mean that the last one you read is the only one that counts -- if the last one is shorter than the first one, the first one will not print completely. Notice also that each integer object knows how big it is, or is supposed to -- it has a .size element that you have been forgetting to set.

    Notice also that you do not have two integer objects, so you cannot read more than one thing at a time. Use a test file with one integer in it, please, and then once you have more than one object you can use a larger file.

  7. #37
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    I took out one string and the program ran perfectly. Now the problem is setting up the program so it can save an array of struct integer so I can save multiple strings...
    Last edited by Graham Aker; 01-25-2009 at 02:59 PM. Reason: Typos

  8. #38
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by Graham Aker View Post
    I took out one string and the program ran perfectly. Now the problem is setting up the program so it can save an array of struct integer so I can save multiple strings...
    Here are some posts of mine with plenty of working sample code for you to learn from:

    how to read strings?

    compiler error

    OS: Linux Mint 13(Maya) LTS 64 bit.

  9. #39
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by happyclown View Post
    Here are some posts of mine with plenty of working sample code for you to learn from:

    how to read strings?

    compiler error

    Thanks! I'll take a look at it immediately!

  10. #40
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Woah, that's a lot of coding to go though... @_____@

  11. #41
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Here's the main code that will interest you.

    The array of structs:
    Code:
    struct data {
            int month, day, year, hour, minute, number;
            char string1[2+1];
            char string2[FILENAMELENGTH];
            char stringstorage[LINELENGTH];  /* Used to store strings from file */
        } LineData[NUMBEROFFILES];
    Then using the function fgets to read strings from a file into the structure.
    Code:
    while((fgets(LineData[counter].stringstorage, LINELENGTH, openfile) != NULL) && (counter < NUMBEROFFILES))
        {
             lastfilenumber = counter;
             counter++;
    		
        }
    Last edited by happyclown; 01-25-2009 at 03:36 PM. Reason: highlighted char stringstorage[LINELENGTH];
    OS: Linux Mint 13(Maya) LTS 64 bit.

  12. #42
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Part of the problem with that is I want to save the string into the array as an integer array, not as a string. Furthermore, part of the assignment was that the structure had to be limited to:
    Code:
    struct integer {
    	int* digits;
    	int size;
    };
    If it wasn't for that second part, I'd try to figure out how to use that code for an integer array over a string, but the limitations on the structure kinda break it. Unless I can somehow do the same thing with this array...

  13. #43
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Graham Aker View Post
    Part of the problem with that is I want to save the string into the array as an integer array, not as a string. Furthermore, part of the assignment was that the structure had to be limited to:
    Code:
    struct integer {
    	int* digits;
    	int size;
    };
    If it wasn't for that second part, I'd try to figure out how to use that code for an integer array over a string, but the limitations on the structure kinda break it. Unless I can somehow do the same thing with this array...
    You've been trying to fit malloc in all over the place, and you don't know how to get memory for an array? Is this some kind of a troll?

  14. #44
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    You've been trying to fit malloc in all over the place, and you don't know how to get memory for an array? Is this some kind of a troll?
    I'm not trying to troll, I'm just a college student who should have payed a lot more attention in class and has been trying to get the same program to work for over a week. I have friends in class who have been helping me out as well, any they're the ones who sugested malloc.

    ...Now that I think about it, I think I know what happyclown meant. Sorry about that last post, just ignore it please.

    EDIT:I dropped malloc and just decided to switch to a basic array for the structure. The result was an error saying I had a "request for member 'digits' in something not a structure or a union", even though it's obviously part of a structure. Here's the new code (The problem line's in red):

    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;
    };
    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;
    };
    Last edited by Graham Aker; 01-25-2009 at 04:13 PM. Reason: I'm an idiot... >_<

  15. #45
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I put your program into my compiler, and here is the error message:

    Code:
     printf("\n The arrays are: ");
          for (k=0; k<stringnum; k++){
               for (i=0; i<j; i++)
                   printf ("%d ", num[k].digits[i]);   <=== error here(line 36)===
          printf("\n");
          }
    (36) : error C2231: '.digits' : left operand points to 'struct', use '->'

    I think you're trying to access the structure using the incorrect syntax.

    EDIT: Also go these warnings

    c(45) : warning C4013: 'strlen' undefined; assuming extern returning int
    c(50) : warning C4013: 'isdigit' undefined; assuming extern returning int

    Use
    Code:
    include <string.h>
    to get of rid of the 'strlen' warning.

    And use
    Code:
    #include <ctype.h>
    to get rid of the 'isdigit' warning.

    You are using these functions without loading the appropriate header files where they are stored.
    Last edited by happyclown; 01-25-2009 at 04:29 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

Page 3 of 7 FirstFirst 1234567 LastLast
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, 09: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, 01:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21