【问题标题】:Making an array containing structures - C制作包含结构的数组 - C
【发布时间】:2014-03-10 22:55:53
【问题描述】:

今天我正在尝试实现一个队列,但它适用于数组中的结构(我一直讨厌 C 'Array of Structs' 术语,因为我不想这样做)。但是,当我尝试进行基本初始化时,遇到如下编译器错误。

“请求成员 '**' 不是结构或联合。”

这是我的代码,目前不多。

//------------------------Preprocessor Instructions. ------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define MAX 128
#define BUFFER 120


//-------------------------Global Stuff -------------------------------------------------
int head=-1;
int tail=-1;    //Starting head and tail at -1. 

struct Entry{
    int Data;
    int Hops;   
}; 
struct Entry Queue[MAX];    //Queue made up of structs.

int visited[MAX];


//------------------------Function Definitions. -----------------------------------------
int QueuePush(struct Entry *q, int num);
int QueuePop(struct Entry *q);
int IsEmpty(struct Entry *q);


//------------------------Main. ---------------------------------------------------------
int main(void)
{
    int i;

    while(i<MAX){
        Queue.Data[i]=0;
        Queue.Hops[i]=0;
        i++;
    }

    for(i=0;i<=10;i++){
        printf("Queue Data[%d] = %d \n", i, Queue[i].Data);
        printf("Queue Hops = %d \n", Queue[i].Hops);
    }


}

我在定义数组的方式上是否犯了一些可怕的大错误?还是语法问题?提前致谢。

【问题讨论】:

    标签: c arrays struct compiler-errors


    【解决方案1】:

    你应该写

    Queue[i].Data = 0;
    Queue[i].Hops = 0;
    

    不是

    Queue.Data[i] = 0;
    Queue.Hops[i] = 0;
    

    【讨论】:

      【解决方案2】:

      在初始化 while 循环中,您编写了 Queue.Data[i] 而不是 Queue[i].Data

      【讨论】:

        【解决方案3】:

        还有一件事:

        int i;
        
        while(i<MAX){
            Queue.Data[i]=0;
            Queue.Hops[i]=0;
            i++;
        }
        

        这里i 包含随机数,这个循环不起作用,所以你应该写int i = 0; 或使用for 而不是while

        【讨论】:

        • 是的,我在大约 30 秒前捕捉到了,哈哈。不过谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多