0

How to solve segmentation error in c

#include <stdio.h> #include <stdlib.h> #include <string.h> void main() { FILE *fint, *ftab, *flen, *fsym; int op1[10], textlen, textlen1, i, j = 0, len; char add[5], symadd[5], op[5], start[10], temp[30], line[20], label[20], mne[10], operand[10], symtab[10], opmne[10]; fint = fopen("input.txt", "r"); flen = fopen("length.txt", "r"); ftab = fopen("optab.txt", "r"); fsym = fopen("symtab.txt", "r"); fscanf(fint, "%s%s%s%s", add, label, mne, operand); if (strcmp(mne, "START") == 0) { strcpy(start, operand); fscanf(flen, "%d", &len); } printf("H^%s^%s^%d\nT^00%s^", label, start, len, start); fscanf(fint, "%s%s%s%s", add, label, mne, operand); while (strcmp(mne, "END") != 0) { fscanf(ftab, "%s%s", opmne, op); while (!feof(ftab)) { if (strcmp(mne, opmne) == 0) { fclose(ftab); fscanf(fsym, "%s%s", symadd, symtab); while (!feof(fsym))

6th Dec 2020, 6:52 AM
Śhádów 💕
Śhádów 💕 - avatar
3 Réponses
+ 5
Might You are getting segmentation fault because you haven't allocated space for elements of str! You need to allocate memory first in main function. Like - scanf("%d",&t); //input the number of test cases if(t <= 10) for(size_t i = 0; i < t; i++) str[i] = malloc(50); // Assuming string is no more than 50characters. else exit(0); Hope this hint helps
6th Dec 2020, 10:03 AM
Piyush
Piyush - avatar
+ 2
Your code is truncated because it is too big to fit in the character limits of thread Description. You should've saved a copy of the code in SoloLearn, and share the saved code link instead. That way you won't have to worry about charaacter limits. https://www.sololearn.com/post/75089/?ref=app
6th Dec 2020, 9:01 AM
Ipang