In my program, i am allocating dyanamic memory to a variable buffer of type 'char *' using malloc. Then if I using strtok(buffer,"+");
it is giving Segmentation fault. I got the reason for this Stackoverflow and same problem Stackoverflow. but neither post giving me a desired solution. As I can't use static memory or array type according to my program.
My problem is In strtok, in arguements if I use char array then working properly and when use char * then give error. How to use char * in strtok arguements.
char *buffer;
int len;
connection_t * conn;
long addr = 0;
FILE *fptr;
if (!ptr) pthread_exit(0);
conn = (connection_t *)ptr;
const char *s = "+";
char *token;
/* read length of message */
read(conn->sock, &len, sizeof(int));
if (len > 0)
{
addr = (long)((struct sockaddr_in *)&conn->address)->sin_addr.s_addr;
buffer = (char *)malloc((len+1)*sizeof(char));
buffer[len] = 0;
/* read message */
read(conn->sock, buffer, len);
printf("%s and %d \n",buffer, addr);
/* get the first token */
token = strtok(buffer,"+");
last line showing segmentation fault