C:传递和返回指向 char* 的指针
C: Passing and returning pointers to a char*
我正在尝试通过多个函数(每个函数都提取消息的一部分)来解析 char*,但在函数之间传递指针时遇到了问题。在我遇到问题的消息部分,有一个整数后跟一个 space 字符,再后跟一个双精度数。
这就是 STM32F4 上的所有 运行:
主要功能:
char* myMsg = NULL;
char* nLink = NULL;
SerialQueue_pop(&myMsg, &tablet_queue); //Extract a char* from a buffer
uint8_t id = extract_gset_id(myMsg, (char*)&nLink); //Extract the integer from the char*
real value = extract_gset_value((char*)&nLink); //Extract the real (float) from the char*
函数:
int8_t extract_gset_id(char* message, char* pEnd)
{
char** ptr;
if ((strlen(message)-13)>0){
int8_t val = (int8_t)( 0xFF & strtol(message+13, &ptr,10));
*pEnd = ptr;
return val;
}
return -1;
}
real extract_gset_value(char* message)
{
if ((strlen(message))>0){
char arr[8];
real val = strtod(message, NULL);
snprintf(arr, 8, "%2.4f", val);
return val;
}
return -1;
}
第一个函数调用应从字符串的第 13 个字符开始提取一个整数。这工作正常,如果我在 strtol 调用后读取 return 指针 (nLink),它指向正确的位置(在整数之后的 space 处)。但是,当我在主函数或第二个函数中从指针读取字符串时,它没有指向正确的位置。
我想做的是让主函数传递一个指向由第一个函数更新的数组的指针,然后第二个函数获取该指针并使用它。
如有任何帮助,我们将不胜感激。
尝试写:
SerialQueue_pop(myMsg, tablet_queue);
uint8_t id = extract_gset_id(myMsg,nLink);
real value = extract_gset_value(nLink);
(我想 tablet_queue 是 char * )。
当你有一个带有参数 char* 和指针 char* p 的函数声明 f 时,你传递的是 f(p) 而不是 f(&p) ,所以当你传递 [=11 时,你可能还需要更改 ptr =].
完整的固定代码,存根以确保其正常工作:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
typedef unsigned char int8_t; // I had to guess
typedef double real;
int8_t extract_gset_id(const char* message, char** pEnd)
{
if ((strlen(message)-13)>0){
int8_t val = (int8_t)( 0xFF & strtol(message+13, pEnd,10));
return val;
}
return -1;
}
real extract_gset_value(const char* message)
{
if ((strlen(message))>0){
//char arr[8];
real val = strtod(message, NULL);
//snprintf(arr, 8, "%2.4f", val); // this line is useless
return val;
}
return -1;
}
int main()
{
char* myMsg = NULL;
char* nLink = NULL;
//SerialQueue_pop(&myMsg, &tablet_queue); //Extract a char* from a buffer
myMsg = "abcdefghijklm129 5678.0";
int8_t id = extract_gset_id(myMsg, &nLink); //Extract the integer from the char*
real value = extract_gset_value(nLink); //Extract the real (float) from the char*
printf("%d, %lf\n",(int)id,value);
}
你的大部分类型都错了,特别是在 extract_gset_id
例程中。
第一个参数是消息指针,OK
第二个参数是内部 strtol
在完成解析整数时设置的 char 指针(设置为输出)上的指针,因此您知道从哪里恢复解析。您必须将指针作为指针传递,以便它可以更改 main
.
中的值
一旦你解析了整数,剩下的就差不多了。请注意,我不需要投任何东西。当您将 char **
转换为 char *
时出现问题。
我正在尝试通过多个函数(每个函数都提取消息的一部分)来解析 char*,但在函数之间传递指针时遇到了问题。在我遇到问题的消息部分,有一个整数后跟一个 space 字符,再后跟一个双精度数。
这就是 STM32F4 上的所有 运行:
主要功能:
char* myMsg = NULL;
char* nLink = NULL;
SerialQueue_pop(&myMsg, &tablet_queue); //Extract a char* from a buffer
uint8_t id = extract_gset_id(myMsg, (char*)&nLink); //Extract the integer from the char*
real value = extract_gset_value((char*)&nLink); //Extract the real (float) from the char*
函数:
int8_t extract_gset_id(char* message, char* pEnd)
{
char** ptr;
if ((strlen(message)-13)>0){
int8_t val = (int8_t)( 0xFF & strtol(message+13, &ptr,10));
*pEnd = ptr;
return val;
}
return -1;
}
real extract_gset_value(char* message)
{
if ((strlen(message))>0){
char arr[8];
real val = strtod(message, NULL);
snprintf(arr, 8, "%2.4f", val);
return val;
}
return -1;
}
第一个函数调用应从字符串的第 13 个字符开始提取一个整数。这工作正常,如果我在 strtol 调用后读取 return 指针 (nLink),它指向正确的位置(在整数之后的 space 处)。但是,当我在主函数或第二个函数中从指针读取字符串时,它没有指向正确的位置。
我想做的是让主函数传递一个指向由第一个函数更新的数组的指针,然后第二个函数获取该指针并使用它。
如有任何帮助,我们将不胜感激。
尝试写:
SerialQueue_pop(myMsg, tablet_queue);
uint8_t id = extract_gset_id(myMsg,nLink);
real value = extract_gset_value(nLink);
(我想 tablet_queue 是 char * )。
当你有一个带有参数 char* 和指针 char* p 的函数声明 f 时,你传递的是 f(p) 而不是 f(&p) ,所以当你传递 [=11 时,你可能还需要更改 ptr =].
完整的固定代码,存根以确保其正常工作:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
typedef unsigned char int8_t; // I had to guess
typedef double real;
int8_t extract_gset_id(const char* message, char** pEnd)
{
if ((strlen(message)-13)>0){
int8_t val = (int8_t)( 0xFF & strtol(message+13, pEnd,10));
return val;
}
return -1;
}
real extract_gset_value(const char* message)
{
if ((strlen(message))>0){
//char arr[8];
real val = strtod(message, NULL);
//snprintf(arr, 8, "%2.4f", val); // this line is useless
return val;
}
return -1;
}
int main()
{
char* myMsg = NULL;
char* nLink = NULL;
//SerialQueue_pop(&myMsg, &tablet_queue); //Extract a char* from a buffer
myMsg = "abcdefghijklm129 5678.0";
int8_t id = extract_gset_id(myMsg, &nLink); //Extract the integer from the char*
real value = extract_gset_value(nLink); //Extract the real (float) from the char*
printf("%d, %lf\n",(int)id,value);
}
你的大部分类型都错了,特别是在 extract_gset_id
例程中。
第一个参数是消息指针,OK
第二个参数是内部 strtol
在完成解析整数时设置的 char 指针(设置为输出)上的指针,因此您知道从哪里恢复解析。您必须将指针作为指针传递,以便它可以更改 main
.
一旦你解析了整数,剩下的就差不多了。请注意,我不需要投任何东西。当您将 char **
转换为 char *
时出现问题。