【问题标题】:Initialising integer array as member将整数数组初始化为成员
【发布时间】:2016-10-29 19:29:46
【问题描述】:

我想用队列类型初始化我的变量。但是我遇到了一些麻烦。警告说指向分配给 int 的整数转换的指针不兼容。这是什么意思?

#include <stdio.h>
#include <stdlib.h>
#define MAX 4

struct queue
{
    int array[MAX];
    int front;
    int back;
};
typedef struct queue Queue;

Queue qInit(Queue table[], int front, int back);

int main(void)
{

    Queue table[MAX];
    int front, back;

    qInit(table, front, back);

    return 0;
}

Queue qInit(Queue table[], int front, int back)
{
    Queue c;

    c.array[MAX]=table;  // <---- getting warning right here.
    c.front=front;
    c.back=back;

    return c;
}

【问题讨论】:

  • 如果数组的大小为MAX,则MAX的索引中没有元素,它只是从0到MAX-1。除此之外,由于 table 是一个整数数组,你不能隐式地将指针(table 是一个数组,因此是一个指针)分配给一个整数点
  • @ZachP 不太清楚你的意思...
  • Alex 不清楚您要做什么。带有警告的行是因为数组用于整数,但您正试图将队列放入其中。 c.array[X] 只能给定一个整数,X 必须是从 0 到 MAX-1,因为 MAX 是 4,数组的有效索引是 0,1,2 和 3,而不是 4。

标签: c struct initialization


【解决方案1】:

问题是(@Zach P 也试图解释)array[MAX] 包含 MAX 个索引为 0 到 MAX-1 的值,并且没有位置 MAX,所以最后一个索引是 array[MAX-1]。

其次,table 是一个指向数组的指针,因此您不能将指针分配给 int 变量(array[MAX-1] 是 int 类型)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2011-05-02
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多