转换字符数组字符串[和卵石]

编程入门 行业动态 更新时间:2024-10-25 18:36:29
本文介绍了转换字符数组字符串[和卵石]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我试图变成一个字符指针为字符串char数组。我相信这涉及获取指向字符数组的第一个元素,并添加一个空字符到字符数组的结束。这样做的原因是,我试图然后将它传递给 SimpleMenuItem 的卵石的SmartWatch,其中 .title伪需要得到一个的char * ,指向一个字串。

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.

虽然我已经能够得到填充字符数组,(我认为)增加了空字符,并得到指针,我无法看到我的卵石称号。我不知道这是否是一个鹅卵石问题或我的C理解的问题,但我觉得很大程度上,这可能是前者。

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.

卵石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); }

如果任何人有,为什么卵石没有显示它是在 .title伪被分配数据的任何建议,我很想听听他们。

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.

谢谢!

推荐答案

解决方案实际上比这要简单得多。

The solution is actually much simpler than that.

首先,请注意,字符数组和指针为字符串其实都是一样的东西。它们都指向第一个字节的地址。在这两种情况下,系统的功能将搜索空字符识别的字符串(strlen的,strcpy的,printf的,等等)的端部。 的char * 和的char [] 是可以互换的。

First, please note that a char array and a pointer to a string are in fact the same things. They are both pointers to the address of the first byte. In both case, the system functions will search for the null character to identify the end of the string (strlen, strcpy, printf, etc). char* and char[] are interchangeable.

当您收到您的 in_received_handler(),指针你得到( dataReceived-&GT数据;值 - &GT;为c_string )指向蓝牙接收缓冲区,你需要到别的地方复制该字符串。这样,每一个屏幕需要时间来重新绘制,人物将可用。

When you receive the data in your in_received_handler(), the pointer you get (dataReceived->value->cstring) points to the bluetooth receive buffer and you need to copy that string somewhere else. This way, every time the screen needs to be redrawn, the characters will be available.

由于您得到项目的动态数量,你必须动态地分配与内存的malloc()。你应该记住以后释放内存(使用免费())。

Because you are getting a dynamic number of items, you have to dynamically allocate the memory with malloc(). You should remember to deallocate that memory later (with free()).

这编译和应该做你想要什么:

This compiles and should do what you want:

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); }

顺便说一句,而不是使用一个字符串键和消息,并都在客户端解析它在C,我会建议使用其他应用程序的消息密钥索引来转移你的钥匙。例如,你可以有:

By the way, instead of using one string with both the key and message and parsing it in C in the client, I would recommend using another app message key index to transfer your 'key'. For example, you could have:

{ 0x1: 33, // the key 0x20: "message" // the actual message }

更多推荐

转换字符数组字符串[和卵石]

本文发布于:2023-11-04 05:43:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:卵石   数组   字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!