简体   繁体   English

在C中将字符串分配给uint8_t

[英]String assignment to uint8_t in c

I am trying to assign string to uint8_t buffer in IAR, but I get a warning message. 我正在尝试将字符串分配给IAR中的uint8_t缓冲区,但收到警告消息。

rx_buffer.rx_struct.RESP.RESPOND is struct field of uint8_t type. rx_buffer.rx_struct.RESP.RESPONDuint8_t类型的struct field

My code is: 我的代码是:

strncpy(rx_buffer.rx_struct.RESP.RESPOND, (uint8_t *)'NS,', 3);

And the associated warning message is the following: 并且相关的警告消息如下:

Warning[Pe1422]: multicharacter character literal (potential portability problem) 
Warning[Pe167]: argument of type "uint8_t *" is incompatible with parameter of type "char *",
Warning[Pe167]: argument of type "uint8_t *" is incompatible with parameter of type "char const"

I have written a workaround : 我写了一个解决方法:

rx_buffer.rx_struct.RESP.RESPOND[0] = 'N';
rx_buffer.rx_struct.RESP.RESPOND[1] = 'S';
rx_buffer.rx_struct.RESP.RESPOND[2] = ',';

But I'm not satisfied with it. 但是我不满意。 What is the correct way to do that? 正确的方法是什么?

Your code is incorrect in many ways: 您的代码在许多方面都不正确:

strncpy(rx_buffer.rx_struct.RESP.RESPOND, (uint8_t *)'NS,',3);
  • The destination array does not have type char * 目标数组没有类型char *
  • the source is not an array: 'NS,' is a multicharacter character constant, a non portable historic oddity that nobody would use in any decent code... casting it to (uint8_t *) does not fix this issue. 源不是数组: 'NS,'是一个多字符字符常量,是一种不可移植的历史奇数,没有人会在任何体面的代码中使用...将其强制转换为(uint8_t *)不能解决此问题。 You should just use double quotes: "NS," . 您应该只使用双引号: "NS,"
  • strncpy() is not the proper tool for this job. strncpy()不是完成此工作的合适工具。 In fact it is never the right tool for any job. 实际上,它绝不是任何工作的正确工具。 This function is not a safe replacement for strcpy , it semantics are widely misunderstood, it is very error prone. 此函数不是strcpy的安全替代品,它的语义被广泛误解,非常容易出错。 You should avoid using this function. 您应该避免使用此功能。 In this particular case, it would just copy the 3 bytes as expected, but why use strncpy() when memcpy() is a simpler solution? 在这种情况下,它只会按预期复制3个字节,但是为什么在memcpy()是更简单的解决方案时使用strncpy()呢?

You can achieve your goal with this: 您可以通过以下方式实现目标:

memcpy(rx_buffer.rx_struct.RESP.RESPOND, "NS,", 3);

or alternately by assigning each byte separately as posted in the question. 或交替分配问题中张贴的每个字节。 Both approaches are likely to produce the same machine code. 两种方法都可能产生相同的机器代码。

strncpy expects its first two arguments to be of type char * and const char * , respectively. strncpy期望其前两个参数分别为char *const char *类型。 Instead of casting to unit8_t * , you should be casting to char * , if at all. 如果没有的话,应该将其转换为char *而不是转换为unit8_t *

Also, 'NS,' is a character literal, a string literal would be "NS," . 此外, 'NS,'是字符文字,字符串文字是"NS,"

You are using single quotes but you need double quotes ( "NS," ) , and take a look to Stop using strncpy already! 您正在使用单引号,但是需要双引号( "NS," ),然后看看已经停止使用strncpy了! , in this case it should work as expected because you don't want the trailing '\\0' , but don't use it. ,在这种情况下,它应该可以按预期工作,因为您不希望尾随'\\0' ,但不要使用它。

Use memcpy or memmove , also (as a question of style) don't use magic numbers like 3 : 使用memcpymemmove ,而且(作为样式问题)也不要使用像3这样的幻数:

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#define MAX_SIZE 3

int main(void)
{
    uint8_t arr[MAX_SIZE];

    memmove(arr, "NS,", MAX_SIZE);
    for (int i = 0; i < MAX_SIZE; i++) {
        printf("%c", arr[i]);
    }
    printf("\n");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM