I was making some proves with strtol() from stdlib library because i had a program that always crashed and i found that this worked perfectly:
main(){
char linea[]="0x123456",**ap;
int num;
num=strtol(linea,ap,0);
printf("%d\n%s",num,*ap);
}
But when I added just a new declaration 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 at executing strtol() can't understand why. I'm doing this on Code::Blocks
You get a crash because you are passing strtol
an uninitialized pointer, and strtol
dereferences it. You do not get a crash the first time by pure luck.
This will not crash:
main() {
char linea[]="0x123456", *ap;
int num;
num = strtol(linea, &ap, 0);
printf("%d\n%s", num, ap);
int k;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With