我有一个 char 数组,我试图将其转换为指向字符串的 char 指针。我相信这涉及获取指向 char 数组的第一个元素的指针,并在 char 数组的末尾添加一个空字符。这样做的原因是我试图将它传递给SimpleMenuItem
pebble 智能手表的 a ,其中.title
需要获取 a char*
,指向一个字符串。
虽然我已经能够填充 char 数组并且(我认为)添加了空字符并获得了指针,但我无法在我的鹅卵石上看到标题。我不确定这是一个鹅卵石问题还是我对 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);
}
如果有人对为什么 pebble 没有显示分配给它的数据有任何建议.title
,我很想听听他们的意见。
谢谢!