Program received signal: “EXC_BAD_ACCESS”?

I am making a command line program in C using XCode. When running the program, it initially does what it is supposed to do (asks me for a file path). However, when I type in a valid and existing file path, it gives me the following error:

Program received signal:  �EXC_BAD_ACCESS�. sharedlibrary apply-load-rules all (gdb) 

I have two warnings in my program, both of which have to do with the function strcat. The warnings are:

warning: implicit declaration of function 'strcat'

and

warning: incompatible implicit declaration of built-in function 'strcat'

I am wondering why my program is not executing properly.

Thanks,
Mike

My code is posted below:

#include 
   #include "stdlib.h"
   int main (void)
   {
       char *string1;
       printf("Type in your file path: ");
       scanf("%s", string1);
       char *string2 = "tar czvf YourNewFile.tar.gz ";
       strcat(string2, string1);
       system(string2);
       
   } 
   
   

EDIT: Maybe it has to do with allocating the chars?

For man strcat (POSIX) to work you'll have to include strings.h too.

It seems to be giving me the error as soon as scanf tries to save my entry.

char *string1;

This is a character pointer. On a 32-bit system, it takes 4 bytes, on a 64-bit system, 8 bytes. When I type in 'slartibartfast', where is it going to put it?

Inside the pointer? No, not enough room, and besides -- that's not how pointers work. The idea of a pointer is that it points to the memory you want to use, right?

So what memory have you got the pointer pointing to? Nothing? Uh-oh.

That leaves it at a random garbage value. Since pointers have no magic power of predicting what you want to do to them, it doesn't create memory for you to put things in and the pointer remains at what it started at -- some random garbage value. So scanf tries to store it in some undefined location in memory which naturally crashes

You should make the pointer actually point to something if you expect it to work. char string1[128]; or char *string1=malloc(128);

A string of characters such as "ABCD" is a character array. That we all know.

But an array evaluates to the address of it's first element, so when you see "ABCD" in an expression, it is effectively treated as a char *.

So the statement

char *string = "ABCD"

is perfectly valid. The pointer "string" is a char * value that is assigned the address of the first element of the "ABCD" array.

The OP's problem is he's trying to concatenate another string on the end of a static, constant array - his strcat() call is trying to write to the memory immediately following where his "tar czvf YourNewFile.tar.gz " string is stored.

And that appears to be read-only memory. Which is better than nuking whatever data is there.

Check again. He's trying to scanf into an uninitialized character pointer(string1).

That said, the problem you noticed will cause it to crash too! :b:

I thought you were commenting on the assignment of an array to a pointer.