Reputation: 193
I have this code but here atoi does not work at all If i gave base address then also it causes prob
int main()
{
char *token;
int value;
char data = "23+100";
int i = 0;
while(data[i] != '+' )
{
value = atoi(data[i]);
push(value,&top1);
i++;
}
}
Upvotes: 0
Views: 375
Reputation: 50776
Have a look at the strtol
function. Following sample shows how to use it.
int main()
{
char *token;
int value;
char *data = "23+100-2*7";
int i = 0;
char *endp ;
token = data ;
do
{
value = strtol(token, &endp, 10) ;
token = endp + 1 ;
printf ("value: %d\n", value) ;
if (*endp != 0)
printf ("operator: %c\n", *endp) ;
} while (*endp != 0) ;
}
Upvotes: 0
Reputation: 399793
This:
char data = "23+100";
really shouldn't have compiled.
It should be:
const char *data = "23+100";
Which will make atoi(data[i]);
even less correct than it was before.
Also, you're not supposed to use atoi()
on sub-strings like that. Perhaps you should look at sscanf()
or strtok()
, or (perhaps best if I understand what you're after correctly) strtol()
.
The latter allows you to parse a long
integer until a non-digit character is found, and also lets you figure out which character that was after the call.
Upvotes: 1
Reputation: 1420
array initialization is wrong it could be either
char data[] = "23+100"
or as unwind told
const char *data = "23+100"
and one more thing your logic for going through the array might be wrong.
As soon as +
is encountered the loop is finished and 100 won't
be converted and push'd onto the stack(hoping your trying to achieve that)
Upvotes: 0