字符串从变量(&变量)地址的长度 - 嵌入式C
Length of the string from address of variable(&variable) - Embedded C
如何从变量(& variable)地址找到字符串的长度?以下是代码:
SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR7, &newValue); // Hello123
const char echoPrompt[] = "Print From BLE characters:rn";
UART_write(uart, echoPrompt, sizeof(echoPrompt)); // Output : Print From BLE characters: | Size : 29
UART_write(uart, &newValue, sizeof(&newValue)); // Output : Hello | Size : 4
我在代码作曲家Studio(CCS)中使用此代码。我需要在UART中打印字符串,我需要在其中指定字符串中的字符数。
我需要打印" Hello123",而不是打印其" Hello"
&newValue
是一个指针,因此sizeof(&newValue)
返回指针的大小,而不是指向的字符串。假设newValue
是一个null终止的字符串,请使用strlen()
。
sizeof
在编译时间运行,它无法获得动态构造的字符串的大小。
您也应该使用echoPrompt
来执行此操作,因为sizeof
包括尾随的null字节,您可能不需要写。
UART_write(uart, echoPrompt, strlen(echoPrompt));
UART_write(uart, &newValue, strlen(&newValue));
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- 当使用Lua作为嵌入式语言(比如c++)时,有什么简单/方便的方法可以找到变量在Lua中的定义位置吗
- 字符串从变量(&变量)地址的长度 - 嵌入式C
- 嵌入式C++:如何使用容器/数组在堆栈上回收变量与在堆上动态分配