In the code below, the line:
*end = *front;
gives a segmentation fault. I asked a similar question here but I'm not sure if this is because I have two copies of num. Please explain why it's seg-faulting. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getPalin(char* num);
int main()
{
char* num = (char*)malloc(100);
num = "123456";
printf("%s\n", getPalin(num) );
return 0;
}
char* getPalin(char* num)
{
int length = strlen(num);
if ( length % 2 == 0 )
{
char* front = num;
char* end = num + strlen(num) - 1; //pointer to end
while( front != num + (length/2) ) //pointers not middle yet
{
*end = *front;
printf("%c", *end);
front++;
end--;
}
}
return num;
}