gpt4 book ai didi

C 指针参数不匹配编译警告

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:40 25 4
gpt4 key购买 nike

来自一个非常生疏的 C 想成为程序员的快速问题。我有以下场景

avr-gcc -Wall -Os -DF_CPU=8000000 -mmcu=atmega328p -c mirf.c -o mirf.o mirf.c: In function ‘mirf_config’: mirf.c:76:20: warning: passing argument 1 of ‘mirf_set_TADDR’ from incompatible pointer type mirf_set_TADDR(&addr); ^ In file included from mirf.c:27:0: mirf.h:52:13: note: expected ‘uint8_t *’ but argument is of type ‘uint8_t (*)[5]’ extern void mirf_set_TADDR(uint8_t * adr);

void mirf_config() 
// Sets the important registers in the MiRF module and powers the module
// in receiving mode
{

uint8 addr[5] = {0xA0,0xA1,0xA2,0xA3,0xA4};
mirf_set_TADDR(&addr); // HERE!!

// Set RF channel
mirf_config_register(RF_CH,mirf_CH);

// Set length of incoming payload
mirf_config_register(RX_PW_P0, mirf_PAYLOAD);

// Start receiver
PTX = 0; // Start in receiving mode
RX_POWERUP; // Power up in receiving mode
mirf_CE_hi; // Listening for pakets

}

void mirf_set_TADDR(uint8_t * adr)
// Sets the transmitting address
{
mirf_write_register(TX_ADDR, adr,5);
}

如何消除警告并将指针正确发送到 5 个字节?

谢谢!!

最佳答案

你需要知道的就在这里:

mirf.h:52:13: note: expected ‘uint8_t ’ but argument is of type ‘int ()[5]’ extern void mirf_set_TADDR(uint8_t * adr);

mirf_set_TADDR 函数需要一个指向 uint8_t 的指针(即 uint8_t *),但您向它传递的是一个数组(即 uint8_t (*)[5])。当传递给函数时,数组会衰减到第一个元素的地址,因此去掉寻址运算符:

mirf_set_TADDR(addr); 

关于C 指针参数不匹配编译警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com