Segmentation fault when appending to strings (char *)
So I'm trying to implement this simple program which essentially parses a simple (untabbed, simple tags) XML file and returns it in a tabbed format, but I've been stuck on trying to append to a string...
Code:
#include <stdio.h>
#include <string.h>
// Echo stdin to stdout with trailing \n newlines
int main(int argc, char**argv) {
char c;
char *str;
int swtch = 0;
c = getchar();
int depth = 0;
while (c != EOF) {
while (c != '\n')
{ str = "";
if (c == '<')
swtch = 0;
else if (c == '>')
{ if (swtch == 0)
depth++;
} else if (c == '/')
depth-=2;
strcpy(str,&c);
}
int i = 0;
while (i < depth)
{ printf(" "); i++; }
printf("%s",str);
putchar(c);
c = getchar();
}
return 1;
}
I've tried strcpy(str,&c), strncpy(str,&c,sizeof(str)), and strlcpy(str,&c,sizeof(str)), but I get a segmentation fault every time..
How do I add one string variable to another?