C is a general-purpose machine-independent computer programming language used for writing operating systems and applications like windows, Git, Oracle database, games, and Python interpreter. Programmers create applications that are used to interact with hardware devices. It is also called a mother language as it works as the base of many other programming languages. It is an old language, but it still has the charm to make programmers use it to create various projects and applications that can create a huge impact. Still, some beginners are learning this language to create applications. When you are working with C programming language, you may get the exception “Error: thread 1: exc_bad_access (code=1, address=0x0)”.
We at Tutopal are ready to help you resolve the error in a simpler way. We leave no stone unturned when it comes to assisting you in solving the error and making it go away. Go ahead with the post to understand the error and solutions. Let’s figure out how the error shows up
How the error pops up
When you try to run a program using Xcode to compare two input files, the error appears. Have a look at the program code you used
typedef struct
{
int start;
int stop;
char *strandID;
} location;
int main(int argc, const char * argv[])
{
if (argc != 4)
{
fprintf(stderr,
"Usage is ./a.out windowfile.txt genefile.txt outputFileName");
exit(-1);
}
//const vars
const char *windowInput = argv[1];
const char *geneInput = argv[2];
const char *outputfile = argv[3];
const int windowHeader = 9;
const int geneHeader = 3;
//get size of structures -- I have debugged and these work correctly, returning the size of my structure
const int posWsize = getSize(windowInput, "+", windowHeader);
const int negWsize = getSize(windowInput, "-", windowHeader);
const int posGsize = getSize(geneInput, "+", geneHeader);
const int negGsize = getSize(geneInput, "-", geneHeader);
//declare structs
location posWindow[posWsize];
location negWindow[negWsize];
location posGene[posGsize];
location negGene[negGsize];
//extract data here
getLocations(posWindow, negWindow, windowInput, windowHeader);
return 0;
}
void getLocations(location *posL, location *negL, const char *input,
const int header)
{
FILE *fileptr = NULL;
fileptr = fopen(input, "r"); //open file
if (fileptr == NULL)
{ //check for errors while opening
fprintf(stderr, "Error reading %s\n", input);
exit(-1);
}
char tmpLoc[20];
char tmpID[2];
int eofVar = 0;
int lineCount = 0;
while (lineCount < header)
{ //skip header and get to data
eofVar = fgetc(fileptr);
if (eofVar == '\n')
lineCount++;
}
int pCount = 0;
int nCount = 0;
while (eofVar != EOF)
{
fscanf(fileptr, "%s %s", tmpLoc, tmpID); //scan in first two strings
if (!strcmp(tmpID, "+"))
{ //if + strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
posL[pCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
posL[pCount].strandID = tmpID;
printf("start=%d\tstop=%d\tID=%s\tindex=%d\n", posL[pCount].start,
posL[pCount].stop, posL[pCount].strandID, pCount);
pCount++;
}
else if (!strcmp(tmpID, "-"))
{ //if - strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
negL[nCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
negL[nCount].stop = atoi(locTok);
negL[nCount].strandID = tmpID;
nCount++;
}
while ((eofVar = fgetc(fileptr)) != '\n')
{
if (eofVar == EOF)
break;
}
}
fclose(fileptr);
}
//dynamic way...same issue -- just replace this with the above if statement and use the create location function
if (!strcmp(tmpID, "+"))
{ //if + strand
int locStart;
int locStop;
locStart = atoi(strtok(tmpLoc, ".."));//tok and get values
locStop = atoi(strtok(NULL, ".."));
posL[pCount] = *createlocation(locStart, locStop, tmpID);
pCount++;
}
location *createlocation(int start, int stop, char *strandID)
{
location *tmp = NULL;
tmp = (location *) malloc(sizeof(location) * 1);
tmp->start = start;
tmp->stop = stop;
tmp->strandID = (char *) malloc(sizeof(char) * 2);
strcpy(tmp->strandID, strandID);
return tmp;
}
As soon as you execute the script, you get the following error in return
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
Now that you have seen the program code that returns the error, check out the next section to solve it
How To Solve the Error “Error: thread 1: exc_bad_access (code=1, address=0x0)”
We have a few really effective solutions to help you resolve the error efficiently.
Solution 1 – Exception breakpoint in Xcode
This is a simple yet useful solution to make the error message go away. You just need to use an exception breakpoint in Xcode.
Whenever a problem appears in the program, debuggers pause because of an exception breakpoint. So, it helps evaluate the state of your program before it completely crashes.
To use exception breakpoint, open breakpoint (CMD+8). Then tap the ‘+’ button that is found in the bottom left, and then select Add Exception Breakpoint. Without any further ado, you just need to leave it this way. For reference, check out the screenshot

Solution 2 – Check the strtok return value
You get the error warning in the code
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
You get a Null pointer in return because of the strtok. If no tokens are left to retrieve, it returned a null pointer. You get null pointer deference as the address code you have is 0x0, which results in the error warning.
In order to solve the error, you can use the XC method. To do that, remove char*argc[] in the main() function. You can also try to remove both arguments to resolve the error.
Conclusion
We highlighted the solutions to fix the error “Error: thread 1: exc_bad_access (code=1, address=0x0)”. You can choose either way according to your project’s current situation.
I hope it helps! I wish you luck!