Thread: Passing an array of structs to a function

  1. #1
    Unregistered
    Guest

    Passing an array of structs to a function

    i'm passing an array of structures to a function for reading values into it, the prototype is

    void read(structure *record);

    In main() i create the array of a structure by using

    structure record[100];

    When i make the call to the function, i get a general protection fault or a blue screen error. any ideas as to why this is happening? here is my call

    read(record);

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Looks alright. You'll have to post more code.
    zen

  3. #3
    Unregistered
    Guest
    this is part of the code, i commented the line that causes the problem. btw, the same problem happens with each different function call, the program started fine when i commented the function calls out.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    /*create the structures for the array*/
    typedef struct 
    {
      char first_name[16];
      char last_name[16];
    } name;
    
    typedef struct 
    {
      name name_struct;
      char city[50];
      char state[2];
      long int zip_code;
    } info_stuff;
    
    FILE *fptr;
    /*function prototypes*/
    void read(info_stuff *record);
    void search(info_stuff *record);
    void print(info_stuff *record);
    int main() 
    {
      info_stuff record[100];
      int option = 0;
    
      /*open a file to read from*/
      if ( ( fptr = fopen("a:\\somefile.txt", "r") ) == NULL )
      {
    	exit(1);
      }
      /*print the menu and let the user choose*/
      while ( option != 4 ) 
      {
        printf("\n1. Read.\n"
               "2. Search.\n"
    	   "3. Print.\n"
    	   "4. End\n"
    	   "Enter your selection: ");
        scanf("%d", &option);
        
        /*call the corresponding function for the option*/
        switch ( option ) {
          case 1:
            read(record);    /* causes general protection fault */
    	break;
            .
            .
            .
            ...etc

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    read(record); /* causes general protection fault */
    Let's see the definition of read() then.
    zen

  5. #5
    Unregistered
    Guest
    Code:
    void read(info_stuff *record) {
      int i = 0, j;
    
      while( !feof( fptr ) ){
        fscanf(in, "%s%s%s%s%ld\n", &record[i].name.last_name, &record[i].name.first_name, 
    &record[i].city, &record[i].state, &record[i].zip_code);
    
        for( i = 0; i < 2; i++ ) {
          if ( record[i].state[j] >= 'a' && record[i].state[j] <= 'z' ) { record[i].state[j]+='A'-'a'; }
        }
        //printf("%s, %s\n%s, %s %ld\n", record[i].name.last_name, 
    record[i].name.first_name, record[i].city, 
    record[i].state, record[i].zip_code);
      }
      i++;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int i = 0, j;
    j is uninitialised....

    > for( i = 0; i < 2; i++ )
    I think you meant j here
    i is your main index through the record array

    > if ( record[i].state[j]
    This is where the uninitialised j really hurts

    > i++;
    This should be inside the while loop, otherwise you'll never use all the record slots.

    In addition, it should be
    Code:
    while( i < 100 &&
              fscanf(in, "%s%s%s%s%ld",
              &record[i].name.last_name,
              &record[i].name.first_name,
              &record[i].city,
              &record[i].state,
              &record[i].zip_code) != EOF ) {
    }
    This handles EOF properly, and prevents you from running off the end of the array.

    One more thing, I think read is a reserved function name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM