简体   繁体   中英

Segmentation Fault, Array of structures

im having alot of trouble with segmentation faults when building array of structures. Earlier i had a program that had an incorrect counter and kept on segmentation faulting but i was able to fix it. With this program however i cant seem to figure out why it keeps on segmentation faulting. Input from the file being read is

Anthony,Huerta,24
Troy,Bradley,56
Edward,stokely,23

i want to read this file,tokenize it,get each token and store it in its own structure inside an array of structures so at the end i can print each element of the structure as in the array. For example i want array[0] to be the structure to have the first name,last name, and age Here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct info {
    char first[20];
    char last[20];
    int age;
};

int tokenize(struct info array[],FILE* in);

int main()
{
    struct info struct_array[100];
    FILE* fp = fopen("t2q5.csv","r");
    int size = tokenize(struct_array,fp);
    int z;
    for(z=0; z < size; z++)
        printf("%s %s %d",struct_array[z].first,struct_array[z].last,struct_array[z].age);
}

int tokenize(struct info array[],FILE* in)
{
    char buffer[20];
    char* token;
    char* first_name;
    char* last_name;
    char* age;
    char* del = ",";
    int number,count,index = 0; 

    while(fgets(buffer,sizeof(buffer),in) != NULL)
    {
        token = strtok(buffer,del);
        first_name = token;
        count = 1;
        while(token != NULL)
        {
            token = strtok(NULL,del);
            if(count = 1)
                last_name = token;
            if(count = 2)
                age = token;
            count = count + 1;
        }
        number = atoi(age);
        strcpy(array[index].first,first_name);
        strcpy(array[index].last,last_name);
        array[index].age = number;
        index = index + 1;
    }
    return index;
}

sorry if its a small bug, i tend to miss them but ive tried finding a index problem or something similar but i cant seem to spot it

Your error occurs when you do the equality checks. if(count = 1) should be if(count == 1) and similarly for count = 2 . Remember that = is for assignment and == for comparison.

In if(count = 1) and if(count = 2) , = operator is used instead of == operator. Here both the condition if(count = 1) and if(count = 2) always results in always true. Because its trying to assign 1 to variable count and 2 to variable count respectively. Then finally these two if conditions are like if(1) and if(2) respectively. As if statement results in TRUE for all non zero values both the condition is getting true and executed in all the times.

To avoid these kind of coding mistakes always keep the constant at left side and variable at right side in logical equal condition, like if(1 == count) . This will give compilation error if = is used mistakenly as assignment to constant is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM