c - 为什么 ROM 字符串没有正确传递?

标签 c microchip mplab mplab-c18

我正在尝试将字符串传递给函数。我已经阅读了很多有关 C18 中 RAM/ROM 字符串的内容,我的代码看起来不错,但它不起作用。我的功能是这样的:

int setBluetoothName (static const rom char *name){
    unsigned int n = 0, k = 0;
    char string[27] = {0};    // ATNAME + 20 caracteres de nombre + EOF
    char command[27] = {0};
    _bit state = USART_INT;

    // Deshabilito interrupciones del USART
    USART_INT_DISABLE;
    strcpypgm2ram(&string, &name);

    // Numero de caracteres del nombre
    for (; string[n] != 0x00 ; ++n);
    if(n > 19) return LENGTH_ERROR;     // si me pase de 20 letras es muy largo

    // Configuro el nombre del Bluetooth
    printf("ATNAME%s", &string);

我这样使用它:

setBluetoothName("Brazo");

在 Proteus 中,我看到只有字母“B”被传递,当我将 ROM 字符串复制到 RAM 时,它简直是一团糟(见附图)。 printf() 的输出仅为 ATNAME,不会打印字符串。

我正在使用 C18 v4.40 和 MPLABX v1.41。 非常感谢您的帮助。

enter image description here

最佳答案

尝试:

strcpypgm2ram(string, name);

printf("ATNAME%s", string);

当你声明一个数组时

char string[27] = {0};

那么变量string指的是数组第一个元素的地址,当你声明一个参数如

int setBluetoothName (static const rom char *name)

那么name就是字符串所在的地址。

当您在其中添加 & 时,您将获得包含数据地址的变量的地址。

关于c - 为什么 ROM 字符串没有正确传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616962/

相关文章:

c - read()/write() 如何与 FIFO 配合使用? - 客户端/服务器程序中的问题

c - Del. and r/w linked List Nodes in Multithreading with RW-locks -> 死锁?

c++ - 从 C++ 程序访问 ASM 文件中的函数?

c - 在 MPLab X IDE 上用 C 语言从 IO 引脚读取和写入

c - XC8编译器错误 "no identifier in declaration"

c - 尝试写入使用 cudaMalloc3D 分配的二维数组时出现“非法内存访问”

c++ - 二进制 * 的无效操作数

c - 在 MPLAB X ide 上编译 c 文件时出错

c - usb_device.c 文件的微芯片问题,出现我无法解决的错误

mplab - xc8 : please detailed the (((unsigned)&(REGISTER_NAME))*8) + BIT_NUM