[英]Making an array containing structures - C
Today I'm trying to implement a queue, but one that works with structures within arrays (I've always hated the C 'Array of Structs' terminology, as I am not trying to make that). 今天,我正在尝试实现一个队列,但该队列可用于数组中的结构(我一直很讨厌C语言的“数组结构”,因为我没有这么做)。 However, when I try and do a basic initialization, I run into the compiler error as follows. 但是,当我尝试进行基本初始化时,遇到如下编译错误。
"Request for member ' * *' in something not a structure or union. " “在非结构或联合体中请求成员' * *'。”
Here's my code, not much as of now. 这是我的代码,到目前为止还不多。
//------------------------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);
}
}
Am I making some scary, large error in the way I'm defining the array? 我在定义数组的方式上是否犯了一些可怕的大错误? Or is the issue syntactical? 还是该问题属于句法? Thanks in advance. 提前致谢。
You should write 你应该写
Queue[i].Data = 0;
Queue[i].Hops = 0;
not 不
Queue.Data[i] = 0;
Queue.Hops[i] = 0;
在初始化while循环时,您编写了Queue.Data[i]
而不是Queue[i].Data
。
And another thing: 还有一件事:
int i;
while(i<MAX){
Queue.Data[i]=0;
Queue.Hops[i]=0;
i++;
}
Here i
contains random number and this loop will not work, so you should write int i = 0;
这里i
包含随机数,此循环将不起作用,因此您应将int i = 0;
写入int i = 0;
or use for
instead of while
. 或使用for
代替while
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.