could anyone explain me why this program will generate a core dump ? something to do with lack of memory allocation ? strcpy maybe ?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * smd_ipv4_vers_decim(char *ipv4)
{
char chaine[4][4];
long num[4];
int i;
char *ch1, *ch2;
char *tmp;
ch2 = strchr(ipv4, ':')+1;
ch1 = strtok (ipv4,":");
strcpy(ch[1], ch1 + (strlen(ch1) - 2));
ch1[2] = '\0';
strcpy(chaine[0], ch1);
strcpy(chaine[3], ch2 + (strlen(ch2) - 2));
ch2[2] = '\0';
strcpy(chaine[2], ch2);
for (i=0 ; i<4 ; i++)
{
num[i] = strtol(chaine[i],NULL,16);
}
sprintf(tmp, "%i.%i.%i.%i", num[0], num[1], num[2], num[3]);
return (tmp);
}
main()
{
char * src= "AC12:D374"; /* A C 1 2 : D 3 7 4 (no smiley !)*/
char * result;
result = smd_ipv4_vers_decim(src);
printf("result: %s\n", result);
return 0;
}