如果代码是用额外函数编写的,则会打印奇怪的行为

时间:2018-01-15 18:02:04

标签: c function printf contiki

我很难在标题中描述问题,但我在这里尝试一下。我有一些变量,我使用printf打印它们。然后我分配第二个变量并打印它。一切都如预期的那样起作用。当我尝试将包括第一个printf的第一行推入函数时,printf会向第二个printf添加一个随机字母。示例如下: 版本1(按预期工作):

main(){
uint8_t len = strlen( (char *)packetbuf_dataptr() );

int16_t channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);//or  integer     value=1
int16_t network_id = packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID);//or  integer value=2
int16_t link_quality = packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY);//or  integer value=3
printf("Main: %d, %d, %d ", channel, network_id, link_quality);
print_message(len);
}
void print_message(uint8_t len){
    char message_v[len];
    packetbuf_copyto(message_v);
    printf("Message: %s\r\n", message_v);
}

结果:

  

主要:146,0,108消息:地球

第二版(几乎相同但是额外的功能)

main(){
uint8_t len = strlen( (char *)packetbuf_dataptr() );
print_variables();
print_message(len);
}

void print_message(uint8_t len){
    char message_v[len];
    packetbuf_copyto(message_v);
    printf("Message: %s\r\n", message_v);
}    

void print_variables(){
int16_t channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);//or  integer value=1
int16_t network_id = packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID);//or  integer value=2
int16_t link_quality = packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY);//or  integer value=3
printf("Main: %d, %d, %d ", channel, network_id, link_quality);
}

结果:

  

主要:146,0,108消息:地球z

0 个答案:

没有答案