I am trying to split a string with strtok()
. I am getting floating numbers as a string and then by using atof()
, i am converting them into floating point and then put them into another array. At least i am trying. My code is like:
int main() {
float new[4];
char inp[16]={"0.2 0.5 0.3 0.7"};
const char s[2] = " ";
char *token;
int i=0,j;
token=strtok(inp,s);
new[i]=atof(token);
i++;
while( token != NULL )
{ token = strtok(NULL, s);
new[i]=atof(token);
i++;
}
for(j=0;j<4;j++)
printf("%f\n",new[j]);
}
But i get errors like:
[Error] expected unqualified-id before 'new'
[Error] expected type-specifier before '[' token
[Error] expected type-specifier before '[' token
[Error] expected type-specifier before '[' token
Why i get these errors? And i couldn't make it work yet but is the logic true? Sorry for poor English and structure mistakes, i am a new user :)