Below is a snippet of my code. My code is supposed to reverse alphabet chars only and skip any special characters. I am currently trying to use "ab-cd" and hope to print "ba-cd", but when I run my code I get a bus error.
I have narrowed down the issue to lines 31 and 32. I malloc'd, but unsure why it will not let me switch chars.
Although it does not show, I will make sure to free the memory using free
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cytype.h> // used for isalpha()
int main(int argc, const char *argv[])
{
// declaring a pointer of type char to tempString
char * tempString = (char *)malloc(sizeof(char)* 6);
tempString = "ab-cd";
char temp;
temp = tempString[0];
tempString[0] = tempString[1];
tempString[1] = temp;
printf("%s\n", tempString);
return 0;
}