以下のコードの問題点は何ですか? 25行目で失敗します。なぜ失敗したのか理解できません。strtokは適切な呼び出しにもかかわらず失敗します
#include <stdio.h>
#include <string.h>
char linebuf[1024]="SET SLMSRVR 10.133.2.102: 50011";
char *tempStr;
char *str;
int main()
{
tempStr = calloc(1024, sizeof(char));
strcpy(tempStr, linebuf+strlen("SET SLMSRVR"));
printf("1 tempStr: %s\r\n", tempStr);
str = strtok(tempStr, ":");
printf("2 tempStr: %s\tstr: %s\r\n", tempStr, str);
if (str != NULL){
printf("Server IP: %s\r\n",str);
} else {
printf("Error 1\r\n");
}
str = strtok(tempStr, NULL);
printf("3 tempStr: %s\tstr: %s\r\n", tempStr, str);
if (str != NULL) {
printf("Port: %s\r\n", str);
}
return 0;
}
ここではstrtokの仕様が示唆するように、第一には、区切り文字列と呼ばれ、次いでNULL
と、両方の場合において、最初の引数の文字列を解析します。 2回目の解析に失敗します。
どのような考えですか?
であり、我々は、ライン#25を数えることになっていますか? –