[英]ERROR :- passing argument 1 of 'get_string' from incompatible pointer type [-Wincompatible-pointer-types]
[英]passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types]
当我使用下面的代码片段时收到这些警告。 谁能帮我解决?
new_str.c: In function ‘main’:
new_str.c:39:25: warning: passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types]
39 | send_to_host(MACRO,ram->s1);
| ~~~^~~~
| |
| stats_t * {aka struct stats_s *}
new_str.c:28:34: note: expected ‘int *’ but argument is of type ‘stats_t *’ {aka ‘struct stats_s *’}
28 | void send_to_host(int macro,int *data)
| ~~~~~^~~~
new_str.c:42:25: warning: passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types]
42 | send_to_host(MACRO,ram->a1);
| ~~~^~~~
| |
| abc_t * {aka struct abc_s *}
new_str.c:28:34: note: expected ‘int *’ but argument is of type ‘abc_t *’ {aka ‘struct abc_s *’}
28 | void send_to_host(int macro,int *data)***
#include <stdio.h>
#include <stdlib.h>
#define MACRO 12
typedef struct stats_s{
int tx;
int rx;
int err;
}stats_t;
typedef struct abc_s{
int a;
int b;
int c;
}abc_t;
typedef struct ram_s{
int dummy;
int dam;
stats_t *s1;
abc_t *a1;
}ram_t;
ram_t ram1;
void send_to_host(int macro,int *data) // 28
{
printf("function called\n");
}
void main(){
ram_t *ram;
ram = &ram1;
send_to_host(MACRO,ram->s1); // 39
send_to_host(MACRO,ram->a1); // 42
}
您需要发送指向 int 的指针(对 int 类型结构成员的引用)而不是取消引用指向 struct 的指针(此指针与参数的类型无关)。
send_to_host(MACRO,&ram -> s1 -> tx); // 39
send_to_host(MACRO,&ram -> a1 -> a); // 42
下面的代码根本不需要。
ram_t *ram;
ram = &ram1;
简单地:
send_to_host(MACRO,&ram1.s1 -> tx); // 39
send_to_host(MACRO,&ram1.a1 -> a); // 42
main
的 function 签名必须是:
int main(void){
/* ... */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.