i'm getting strange behavior out of atoi command. trying find 2 values out of range format [1:2] string being created done dynamic string allocating macro (if sasprintf throws you) read in file @ projects end, however. anyway, seem parsing string correctly, given correct values of token , token2. i'm confused, however, why calling atoi on token2 give me gibberish answer. also, found out in midst of strtok deprecated, haven't bothered switching yet, until solve bug.
char *token; char *token2; int lsb = 0; int msb = 0; char *str = null; sasprintf(str,"[4:0]"); token = strtok(str,"["); if(token != null) { token = strtok(token,":"); msb = atoi(token); printf("%d\n", msb); token2 = strtok(null,"]"); puts(token2); lsb = atoi(token2); printf("%d\n",token2); } output
4
0
19853443
i think, need change
printf("%d\n",token2); to
printf("%d\n",lsb); token2 char * , cannot print using %d. invokes undefined behavior
that said, check return value of strtok() against null. also, strtod() better alternative atoi().
Comments
Post a Comment