Okay, I know there is another question about "exc_bad_access" here, but that seems to deal with Objective-C and iPhone dev., while mine is only regular C. I am new to C and am almost done with my first program until this error showed up. I have been trying to figure it out for a couple of days and am going insane. Any help is appreciated. buggy function:
void edit (int i){
char* z;
char* y;
char compare1[] = "on bobbin\b\b\b\b";
char compare2[] = "not on bobbin";
char compare3[] = "have\b\b\b\b\b\b";
char compare4[] = "don't have";
char wrapedit[] = "wrapped";
char haveedit[] = "have";
char editing[9];
FILE *wrappedlist = fopen("../../wrapped", "r+");
FILE *havelist = fopen("../../havelist", "r+");
fseek(wrappedlist, i*14, SEEK_SET);
fseek(havelist, i*11, SEEK_SET);
printf("Edit? (y=yes, n=no)");
fgets(z, 2, stdin);
if ((*z=='y') && (strncmp(haveslist[i], compare4, (size_t)1) == 0)) {
printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
fgets(y, 2, stdin);
if (*y=='y') {
fputs(compare3, havelist);
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*y=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
printf("Invalid input.");
return;
}
else if ((*z=='y') && (strncmp(haveslist[i], compare3, (size_t)1) == 0)) {
fpurge(stdout);
printf("Edit \"wrapped\" or \"have\"?");
fpurge(stdin);
fgets(editing, 9, stdin);
len = strlen(editing);
editing[len-1]='\0';
if (strcmp(editing, wrapedit)==0) {
if (strncmp(wrapped[i], compare1, (size_t)1)==0) {
printf("Switch \"on bobbin\" to \"not on bobbin\"? (y=yes, n=no)");
fgets(y, 2, stdin);
if (*y=='y') {
fputs(compare2, wrappedlist);
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*y=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
}
else if(strncmp(wrapped[i], compare2, (size_t)1)==0){
fpurge(stdout);
printf("Switch \"not on bobbin\" to \"on bobbin\"? (y=yes, n=no)");
fgets(y, 2, stdin);
if (*y=='y') {
fwrite(compare1, (size_t)strlen(compare1), 1, wrappedlist);
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*y=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
fpurge(stdout);
printf("Invalid input.");
}
fpurge(stdout);
printf("You don't want to edit wrapped apparently.");
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(strcmp(editing, haveedit)==0){
if (strncmp(haveslist[i], compare3, 1) == 0){
printf("Switch \"have\" to \"don't have\"? (y=yes, n=no)");
fgets(y, 2, stdin);
if (*y=='y') {
fputs(compare4, havelist);
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*y=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
printf("Invalid input.");
}
else if(strncmp(haveslist[i], compare4, 1)==0){
printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
fgets(y, 2, stdin);
if (*y=='y') {
fputs(compare3, havelist);
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*y=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
}
printf("Invalid input.");
}
printf("Not editing.");
fclose(wrappedlist);
fclose(havelist);
return;
}
else if(*z=='n'){
fclose(wrappedlist);
fclose(havelist);
return;
}
printf("Invalid entry");
fclose(havelist);
fclose(wrappedlist);
return;
}
I can input a char to the fgets after the "Edit?" prompt, but then I get the exc_bad_access error. Please help, thanks.code:
It happens because your pointers point to no memory.
char* z; // pointer points nowhere
/* snip */
fgets(z, 2, stdin); // writing to pointer that points nowhere: boom!
This tries to put into z
the two next characters from stdin
. However, z
points to nowhere useful. You need it to point to existing memory: declaring a pointer alone isn't enough to get memory next to it.
You probably want a buffer of 2 characters for it:
char z[2];
With this code, z
will be a pointer to enough memory for 2 characters. (In C, an array can be passed wherever a pointer is expected.)
I didn't look too far into your code, but you'll have the same problem for y
.
You haven't allocated any memory for z
or y
and they're left uninitialized so fgets
is writing to random addresses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With