I'm getting a segmentation fault when I try to strstr.
Am I being dumb and missing a blatant code error?
I'm pretty new to the C language so keep that in mind if you see the mistake.
I'll include everything I think you'll need in here.
where the student structure is defined
Code:
typedef struct {
char Nome[64];
char Cognome[64];
char Matricola[64];
} student;
Where the values get inserted:
Code:
int w_input_handler (void) {
int records = 0;
int index = 0;
printf("\nHow many students would you like to add?\n");
scanf("%d", &records);
file_array = (student *) malloc(records * sizeof(student));
if (file_array == NULL) {
printf("\nMemory allocation error\n");
return -1;
}
for (index = 0; index < records; index++) {
printf("\nEnter name of person %d: ", (index+1));
scanf("%s", (*(file_array + index)).Nome);
printf("\nEnter surname of person %d: ", (index+1));
scanf("%s", (*(file_array + index)).Cognome);
printf("\nEnter student id of person %d: ", (index+1));
scanf("%s", (*(file_array + index)).Matricola);
}
new_text_file = fopen(o_option_argument, "ab");
if (new_text_file) {
for(index = 0; index < records; index++) {
return_value = fwrite((file_array + index), sizeof(student), 1, new_text_file);
}
}
}
Here's where I'm getting the segmentation fault on the strstr function:
the *input that gets passed as the value here is just a basic (find_name was declared as a pointer, of course)
Code:
scanf("%s", find_name);
Code:
int find_name_function(char *input) {
int records = 30;
int index = 0;
int number_of_matches = 0;
int return_value;
char *ret = NULL;
student *read_file_array = (student *) malloc(records * sizeof(student));
new_text_file = fopen(o_option_argument, "rb");
if (new_text_file) {
while (1) {
return_value = fread((read_file_array + index), sizeof(student), 1, new_text_file);
if (return_value != 1) {
break;
}
ret = strstr(((*(read_file_array + (index))).Nome), input);
if (ret) {
printf("\nFound student:\n");
printf("\nName >%s<\n", (*(read_file_array + (index))).Nome);
printf("\nSurname >%s<\n", (*(read_file_array + (index))).Cognome);
printf("\nMatricola >%s<\n", (*(read_file_array + (index))).Matricola);
number_of_matches++;
}
index++;
}
}
I'm sorry if it seems a bit confusing or anything like that, I'd gladly give anything else you might want