i am writing a brainf**k interpreter for fun, but i'm getting a segmentation error and i do not understand why, could anyone tell me, please? i am quite new to c programming.
this is the c source code:
Code:
#include <stdio.h>
int main(int argc, char *argv[]){
int i;
int a[30000];
int p;
int c;
if (argc == 1){
printf("Usage: %s filename\n", argv[0]);
}
else {
FILE *file = fopen(argv[1], "r");
if (file == 0){
printf("Error: Could not open file\n");
}
else {
int length;
while(fgetc(file) != EOF){
length++;
}
for(i = 0; i < length; i++){
fseek(file,i,SEEK_SET);
switch(fgetc(file)){
case 60:
p--;
break;
case 62:
p++;
break;
case 43:
a[p] += 1;
break;
case 45:
a[p] -= 1;
break;
case 46:
putchar(a[p]);
break;
case 44:
a[p] = getchar();
break;
case 91:
c = i;
if(a[p] == 0){
for(i = i; i < length; i++){
fseek(file,i,SEEK_SET);
if(fgetc(file) == 93){
i = ftell(file);
break;
}
}
}
break;
case 93:
if(a[p] == 0){
break;
}
else {
c++;
i = c;
break;
}
}
}
}
}
return 0;
}
and this is the code i am trying t execute:
Code:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.