警告有关从兼容的指针类型的分配使用指针和数组时? [英] Warning about assignment from incompatible pointer type when using pointers and arrays?

查看:163
本文介绍了警告有关从兼容的指针类型的分配使用指针和数组时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于结构

typedef struct sharedData
{
    sem_t *forks;
}sharedData;

我得到一个警告,当我尝试这样做:

I get a warning when I try to do this:

sharedData sd;
sem_t forks[5];
sd.forks = &forks; // Warning: assignment from incompatible pointer type

我误解或丢失的东西吗?

Am I misunderstanding or missing something?

推荐答案

的问题是,&放大器;叉的类型为

sem_t (*)[5]

这是一个指针到五年 sem_t 组成的数组。编译器警告是因为 sd.forks 的类型为 sem_t * ,两个指针类型不兑换一另一回事。

That is, a pointer to an array of five sem_ts. The compiler warning is because sd.forks has type sem_t*, and the two pointer types aren't convertible to one another.

要解决这个问题,只是改变了分配

To fix this, just change the assignment to

sd.forks = forks;

因为C的指针/阵列的互换性,为打算在code会工作。这是因为会被视为&放大器;叉[0] ,里面确实有键入 sem_t *

Because of C's pointer/array interchangeability, this code will work as intended. It's because forks will be treated as &forks[0], which does have type sem_t *.

这篇关于警告有关从兼容的指针类型的分配使用指针和数组时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆