Thread: Filling an Array of Structures

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Filling an Array of Structures

    Hello,

    In the project I'm currently working on, I'm trying to fill up an array of structures from stdin. The input contains names, and I want to put each name into an array of structures. Each structure has a name and an int associated with it. Here is what I have:

    Code:
     #include <stdio.h>
     #include <string.h>
     
     #define MAX_PERSON 100
     
     struct person {
        char* name;
        int num;
     };
     
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        int lineNum = 1;
        int pos = 0;
        FILE *fp;
        char *names[MAX_PERSON];
        struct person people[MAX_PERSON];
        
        while ( fgets(buf,BUFSIZ,stdin) != NULL ) {
           tok = strtok(buf," ");
           people[pos] = {tok,lineNum};
           pos++;
           tok = strtok(NULL,"\n");
           people[pos] = {tok,lineNum};
           pos++;
           lineNum++;
        }
        
        
     }
    When I try to compile this, it says there is a parse error before the first { in the lines with people[pos] on it. I'm not entirely sure how to insert an entry into the array of structures. Can someone help me?

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    I fixed a few things, left stuff I had no idea why you did what you did.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     #define BUFSIZ 256
    #define MAX_PERSON 100
    #define NM_SIZE 80 
     struct person {
        char name[NM_SIZE];
        int num;
     };
     
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        int lineNum = 1;
        int pos = 0;
        FILE *fp;
        char *names[MAX_PERSON];  /* what is this for ? */
        struct person people[MAX_PERSON];
        
        while ( fgets(buf,BUFSIZ,stdin) != NULL ) {
           tok = strtok(buf," ");
           memset(people[pos].name,0x00,NM_SIZE);
           strcpy(people[pos].name = {tok,lineNum};     
           tok = strtok(NULL,"\n");
           people[pos].num =atoi(tok); 
           pos++;
           lineNum++;
        }
        
        
     }

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, here's what I have now. It compiles correctly, but I get a Segmentation Fault when I run it.

    Code:
    #include <stdio.h>
     #include <string.h>
     
     #define MAX_PERSON 100
     
     struct person {
        char* name;
        int num;
     };
     
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        int lineNum = 1;
        int pos = 0;
        int i;
        FILE *fp;
        struct person people[MAX_PERSON];
        
        while ( fgets(buf,BUFSIZ,stdin) != NULL ) {
           tok = strtok(buf," ");
           strcpy(people[pos].name,tok);
           people[pos].num = lineNum;
           pos++;
           tok = strtok(NULL,"\n");
           strcpy(people[pos].name,tok);
           people[pos].num = lineNum;
           pos++;
           lineNum++;
        }
        
        for (i = 0; i < pos; i++) {
           printf("%s %d\n", people[i].name,people[i].num);
        }
     }
    Any ideas why I'm getting a segmentation fault?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    It seems to be in the strcpy part of the code.

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    struct person {
        char* name;
        int num;
     };
    That's why

    change char *name;
    to char name[80]; -- or whatever

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, I'm filling up the array correctly. Now I need to qsort and then do a binary search on it. However, I'm having difficulty using bsearh. Here is my code:

    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
     
     #define MAX_PERSON 100
     #define MAX_NAME 10
     
     struct person {
        char name[MAX_NAME];
        int num;
     };
     
     int compare(const void *p, const void *s) {
        return strcmp(((struct person*)p)->name, ((struct person*)s)->name);
     }
     
     int compare2(const void *p, const void *s) {
        return strcmp((char *)p, ((struct person*)s)->name);
     }
     
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        char *result;
        char lookup[3];
        int lineNum = 1;
        int pos = 0;
        int i;
        FILE *fp;
        struct person people[MAX_PERSON];
        struct person *match;
        
        while ( fgets(buf,BUFSIZ,stdin) != NULL ) {
           tok = strtok(buf," ");
           strcpy(people[pos].name,tok);
           people[pos].num = lineNum;
           pos++;
           tok = strtok(NULL,"\n");
           strcpy(people[pos].name,tok);
           people[pos].num = lineNum;
           pos++;
           lineNum++;
        }
        
        qsort(people,pos,sizeof(struct person), compare);
        
        if ( (fp = fopen(argv[1],"r")) == NULL) {
           fprintf(stderr,"Cannot open %s\n",argv[1]);
           exit(1);
        }
        
        while ( fgets(buf,BUFSIZ,fp) != NULL ) {
           tok = strtok(buf," ");
           strncpy(lookup,tok,3);
           match = (struct person*)bsearch(lookup,people,pos,sizeof(struct person),compare2);
           printf("%s",match->name);
        }
           
           
        for (i = 0; i < pos; i++) {
           printf("%s %d\n", people[i].name,people[i].num);
        }
     }
    I have no idea if I'm using bsearch right or not. What I want to do is take lines from a file like this:

    Ann: Ken Jim Ian Len
    Bev: Len Ian Jim Ken
    Cat: Ken Ian Jim Len
    Dot: Ian Len Ken Jim
    Ian: Bev Cat Ann Dot
    Jim: Dot Cat Ann Bev
    Ken: Bev Cat Ann Dot
    Len: Dot Bev Ann Cat

    Then I want to bsearch through my people array and get the number that goes with that person. Can someone tell me how to use bsearch right? I know I'll have to use strtok more than once in the second while loop, but I was just testing bsearch. I'm getting a segmentation fault at the moment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM