Strange crash with strtol () in C

I did some evidence with strtol () from the stdlib library because I had a program that always crashed and I found that it worked perfectly:

main(){ char linea[]="0x123456",**ap; int num; num=strtol(linea,ap,0); printf("%d\n%s",num,*ap); } 

But when I added only a new ad no matter where it crashed, like this

 main(){ char linea[]="0x123456",**ap; int num; num=strtol(linea,ap,0); printf("%d\n%s",num,*ap); int k; } 

just adding that final "int k;" the program crashed when strtol () was executed, I can’t understand why. I do it on Code :: Blocks

+4
source share
1 answer

You get a failure because you pass strtol uninitialized pointer and strtol separates it. You will not get an accident for the first time on sheer luck.

This will not work:

 main() { char linea[]="0x123456", *ap; int num; num = strtol(linea, &ap, 0); printf("%d\n%s", num, ap); int k; } 
+9
source

Source: https://habr.com/ru/post/1413083/


All Articles