I have a char array which I am trying to turn into a char pointer to a string. I believe this involves getting the pointer to the first element of the char array, and adding a null character to the end of the char array. The reason for this is that I am trying to then pass it to a SimpleMenuItem
for the pebble smartwatch, where .title
needs to get a char*
, pointing to a string.
While I've been able to get the char array filled and (I think) added the null character and gotten the pointer, I am unable to see the title on my pebble. I'm not sure whether this is a pebble issue or an issue of my C understanding, but I feel heavily that it may be the former.
Pebble code (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);
}
If anyone has any suggestions for why the pebble isn't displaying the data it is being assigned in .title
, I would love to hear them.
Thanks!