0

文字列への char ポインターに変換しようとしている char 配列があります。これには、char配列の最初の要素へのポインターを取得し、char配列の末尾にnull文字を追加することが含まれると思います。この理由は、文字列を指す を取得する必要がSimpleMenuItemある小石スマートウォッチの にそれを渡そうとしているためです。.titlechar*

char配列を埋めることができ、(私が思うに)null文字を追加してポインターを取得できましたが、小石にタイトルが表示されません。これが小石の問題なのか、私のCの理解の問題なのかはわかりませんが、前者かもしれないと強く感じています。

小石コード (C):

void in_received_handler(DictionaryIterator *received, void *context) {
    dataReceived = dict_read_first(received);
    APP_LOG(APP_LOG_LEVEL_DEBUG, "read first");

    while (dataReceived != NULL){

        if (dataReceived->key == 0x20) {

            //original is of the format "# random string", i.e. "4 test name"
            char* original = dataReceived->value->cstring;
            APP_LOG(APP_LOG_LEVEL_DEBUG, original);

            char originalArray[strlen(original)];
            //copy over to originalArray
            for (unsigned int i = 0; i < strlen(original); i++) {
                originalArray[i] = original[i];
            }

            char* keysplit = strtok(originalArray, " ");
            APP_LOG(APP_LOG_LEVEL_DEBUG, keysplit);

            //key
            int key = atoi(keysplit);
            APP_LOG(APP_LOG_LEVEL_DEBUG, "Int Key: %d", key);

            //good until here
            char remainderArray[sizeof(originalArray)-strlen(keysplit) +1];

            //assign rest of string to new array
            for (unsigned int i = 1; i < sizeof(remainderArray)-1; i++){
                APP_LOG(APP_LOG_LEVEL_DEBUG, "Character             : %c", originalArray[i+strlen(keysplit)]);
                remainderArray[i] = originalArray[i+strlen(keysplit)];
                APP_LOG(APP_LOG_LEVEL_DEBUG, "Character in new Array: %c", remainderArray[i]);
            }

            remainderArray[sizeof(remainderArray)-1] = '\0';

            //data is sucesfully placed into remainderArray
            char* ptr = remainderArray;
            strncpy(ptr, remainderArray, sizeof(remainderArray)+1);
            ptr[sizeof(remainderArray)+1] = '\0';

            chats[key] = (SimpleMenuItem){
                // You should give each menu item a title and callback
                .title = &remainderArray[0],
                .callback = selected_chat,
            };

        }

        dataReceived = dict_read_next(received);
        APP_LOG(APP_LOG_LEVEL_DEBUG, "read again");

    }

    layer_mark_dirty((Layer *)voice_chats);
}

で割り当てられているデータが小石に表示されない理由について何か提案がある場合は.title、ぜひ聞いてください。

ありがとう!

4

2 に答える 2

1

解決策は実際にはそれよりもはるかに簡単です。

まず、char 配列と文字列へのポインタは実際には同じものであることに注意してください。どちらも最初のバイトのアドレスへのポインタです。どちらの場合も、システム関数はヌル文字を検索して、文字列の末尾を識別します (strlen、strcpy、printf など)。char*交換char[]可能です。

でデータを受信すると、取得しin_received_handler()たポインター ( dataReceived->value->cstring) が Bluetooth 受信バッファーを指し、その文字列を別の場所にコピーする必要があります。このようにして、画面を再描画する必要があるたびに、キャラクターが利用可能になります。

動的な数の項目を取得しているため、動的にメモリを割り当てる必要がありますmalloc()。後でそのメモリの割り当てを解除することを忘れないでください( を使用free())。

これはコンパイルされ、あなたが望むことをするはずです:

void in_received_handler(DictionaryIterator *received, void *context) {
  Tuple *dataReceived = dict_read_first(received);

  while (dataReceived != NULL){
    if (dataReceived->key == 0x20) {
      //original is of the format "# random string", i.e. "4 test name"
      char* original = dataReceived->value->cstring;
      APP_LOG(APP_LOG_LEVEL_DEBUG, original);

      char* keysplit = strtok(original, " ");
      APP_LOG(APP_LOG_LEVEL_DEBUG, keysplit);

      //key
      int key = atoi(keysplit);
      APP_LOG(APP_LOG_LEVEL_DEBUG, "Int Key: %d", key);

      // This will return the second part of the string.
      char *message = strtok(NULL, " ");
      // Allocate some memory to hold a copy of that message
      char *copyOfMessage = malloc(strlen(message));
      // And copy the message from the bluetooth buffer to the new memory
      strcpy(copyOfMessage, message);
      APP_LOG(APP_LOG_LEVEL_DEBUG, "Message: %d", key);

      chats[key] = (SimpleMenuItem){
        // You should give each menu item a title and callback
        .title = copyOfMessage,
        .callback = selected_chat,
      };
    }

    dataReceived = dict_read_next(received);
  }
  layer_mark_dirty((Layer *)voice_chats);
}

ところで、キーとメッセージの両方で 1 つの文字列を使用してクライアントで C で解析する代わりに、別のアプリ メッセージ キー インデックスを使用して「キー」を転送することをお勧めします。たとえば、次のようにすることができます。

{
  0x1: 33,                 // the key
  0x20: "message"           // the actual message
}
于 2014-07-07T20:57:18.587 に答える