hi
I have the code below
Code:
void get_value(char *url_str,int count)
{
char *var[64],*tmp[64],*str;
int len,i=0,j,l;
len=strlen(url_str);
str=(char *)malloc(len*sizeof(char));
strncpy(str,url_str,len);
l=strlen(str);
printf("CALLOC STRING : %s \nBellek Uzunlugu : %d\n",str,l);
printf("URL STRING : %s \nBellek Uzunlugu : %d\n",url_str,len);
for( j=count;j>0;j--)
{
if (!i)
tmp[i]=strtok(str,"&");
else
tmp[i]=strtok((char *)NULL,"&");
i++;
}
for (j=0;j<count;j++)
{
var[j]=strtok(tmp[j],"=");
var[j]=strtok((char *)NULL,"=");
}
free(str);
printf("tmp :\n1)%s\n2)%s\n3)%s\n4)%s\n",tmp[0],tmp[1],tmp[2],tmp[3]);
printf("\nvar :\n1)%s\n2)%s\n3)%s\n4)%s\n",var[0],var[1],var[2],var[3]);
}
the code above prints the variable and their values coming from an URL encoded string. (like host=localhost&user=default...)
count is the number of "var=value" pairs.
When I run the code, always first var name is not printed! But if I add at least 30 to len it works fine :/ confused?!?
I get the lenght of string and then allocate a buffer for it but it does not print all the data. Why?