c - Why __lib_start_main appear in array of string -
following code , example strok
#include "stdio.h" #include "string.h" #include "stdlib.h" #define number_of_strings 10 int main(){ char str[] = " select cid acn acn=:c1 , acctname=:c2#/rows=30/using=(c1=70,c2='od100s')"; char *strs[number_of_strings]; int = 0; (char *p = strtok(str," "); p != null; p = strtok(null, " ")) { if(i < number_of_strings){ strs[i] = malloc(strlen(p)+1); strcpy(strs[i], p); i++; } else { break; } } for(i = 0 ; < number_of_strings ; i++){ if(strs[i] != null) printf("%s\n",strs[i]); } return 0; }
when print array of strs ,
[root@prf01 /]# ./test select cid acn acn=:c1 , acctname=:c2#/rows=30/using=(c1=70,c2='od100s') __libc_start_main
i have no idea why "__libc_start_main" string store in array
please me clear ,thanks !
you reading uninitialized pointer(s) undefined behaviour.
you have i
number of strings after loop, not number_of_strings
strings.
you can store total number of strings in variable after loop , use when print it.
(char *p = strtok(str," "); p != null; p = strtok(null, " ")) { .... } size_t num = i; for(i = 0 ; < num ; i++){ if(strs[i] != null) printf("%s\n",strs[i]); }
Comments
Post a Comment