3
const char *sentence = "He was not in the cab at the time.";

printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
    int i = 0;
     int countSpaces = 0;

    while (sentence[i] != '\0') {
        if (sentence[i] == 0x20) {
            countSpaces++;
        }
        i++;
    }    
    return countSpaces;
});

このコードは単純に文字列内の空白をカウントしますが、何らかの理由で 8 ではなく 1606416608 と表示されます。何が問題なのか正確にはわかりません。助けてくれてありがとう!

4

1 に答える 1

9

printfブロックの結果ではなく、実際のブロックを に渡しています。代わりに、試してください

const char *sentence = "He was not in the cab at the time.";

printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
    int i = 0;
    int countSpaces = 0;

    while (sentence[i] != '\0') {
        if (sentence[i] == 0x20) {
            countSpaces++;
        }
        i++;
    }    
    return countSpaces;
}()); // <-- note the extra parentheses here, indicating that you're calling the block
于 2012-06-16T19:27:12.040 に答える