I have following thing which i wanted to convert to int.
char *ptr; // this can point to variable length of string
int balance = functionToConverIntoint(ptr)
So is there any such function in C 开发者_开发知识库"functionToConverIntoint" which can do this job?
Check out strtol() and strtoul().
You want to avoid atoi() as it does not have a good way of distinguishing between a string of "0" and an invalid number.
Yes. atoi
is a basic one with very limited error handling capability; strtol
is a better one.
There's also sscanf()
.
balance = atoi(ptr)
精彩评论