How to use atof in C

0

Could you help me use the atof function? My code is giving "access violation" on the line in which I am using atof, I already tried to change the type of array "given" for both int and float

int main() {
char pilha[10];
int i;
int dado[10];
int topo = 0;

printf("Informe a expressao\n");
gets(pilha);
fflush(stdin);

for (i = 0; i < strlen(pilha); i++)
{
    if (isdigit(pilha[i]))
    {
        dado[topo] = atof(pilha[i]);
        topo++;
    }
}
    
asked by anonymous 31.03.2018 / 15:04

1 answer

1

The atof function is used to convert a string to a double. Strings in C end with '\ 0' and when you do atof (stack [i]) the program does not find '\ 0', which would indicate that it is a string, and generates this error, see in the signature of the function: double atof (const char * str); Use the strtok () function to separate your stack variable into multiple strings according to a separation pattern, the function's signature is: char * strtok (char * str, const char * delim), where char * str is the string you want separate and const char * delim is the string that has what will be used to do this separation. Example usage:

#include <string.h>
#include <stdio.h>
int main(){
   char str[80] = "Isto eh - um - teste";
   char s[2] = "-";
   char *token;
   /* pega o primeiro token */
   token = strtok(str, s);
   /* caminha através de outros tokens */
   while( token != NULL ){
      printf( " %s\n", token );
      token = strtok(NULL, s);
   }
   return(0);
}
    
31.03.2018 / 16:11