문제

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top