C 需要创建指向结构的指针数组吗?

C 需要创建指向结构的指针数组吗?,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,我创建了一个这样的结构 struct PCB { int used; char PID[1]; struct RCB *Other_Resources; char type[10]; struct PCB **list; struct PCB *parent; struct PCB *children; struct PCB *next; int priority; }; struct PCB *PCBLIST[1024]; 然后我声明了

我创建了一个这样的结构

struct PCB
{
   int used;
   char PID[1];
   struct RCB *Other_Resources;
   char type[10];
   struct PCB **list;
   struct PCB *parent;
   struct PCB *children;
   struct PCB *next;
   int priority;
};
struct PCB *PCBLIST[1024];
然后我声明了一个struct PCB*数组,如下所示

struct PCB
{
   int used;
   char PID[1];
   struct RCB *Other_Resources;
   char type[10];
   struct PCB **list;
   struct PCB *parent;
   struct PCB *children;
   struct PCB *next;
   int priority;
};
struct PCB *PCBLIST[1024];
我的问题是我是否需要malloc结构指针数组才能使用这些指针?我在这里读到另一个问题,我应该这样做:

PCBLIST = malloc(1024 * sizeof(struct PCB *));
这稍微修改了一下,我想在最初的问题中,他们使用了MAX,而不是像我那样给出实际值

我从gcc c99得到的错误是:

error: assignment to expression with array type

但如果我试图访问单个结构指针,比如PCBLIST[I]->used,我会遇到seg错误。有人能解释一下我做错了什么吗?

您不需要动态分配数组,因为数组声明已经分配了数组。您需要为要指向的每个数组项分配结构内存。比如:

int ix;
for (ix = 0; ix < sizeof(PCBLIST) / sizeof(PCBLIST[0]); ix++) {
    PCBLIST[ix] = malloc(sizeof(struct PCB));
}

这不需要动态分配,从而简化了事情。但代价是可能会使用更多内存,因为它不允许稀疏填充的数组(不是所有已分配的条目)。最好使用哪一个取决于您打算如何使用阵列。

不,您不需要
malloc()
。您可以初始化指针以指向结构的静态实例。如果使用动态分配,则应使用
calloc()
,而不是
malloc()
plus算术。

PCBLIST应该是指针,而不是数组,因为您希望使用malloc分配空间,并将地址存储到指针

您需要分配结构的大小,而不是指针的大小

struct PCB *PCBLIST;

PCBLIST = malloc(1024 * sizeof(struct PCB));

编辑:使用malloc是选择的问题,不是必须的

下面是一些使用未动态分配的静态节点的示例

在第一个问题中,我没有更改您在问题中定义的位(因此您确实有一个指向PCB对象的指针数组-我认为这不是您想要的,因为如果您仅创建您使用的节点,则used标志没有用处:

#include <string.h> // strcpy

// the PCB structure definition
struct PCB
{
   int used;
   char PID[1];
   struct RCB *Other_Resources;
   char type[10];
   struct PCB **list;
   struct PCB *parent;
   struct PCB *children;
   struct PCB *next;
   int priority;
};

// an uninitialized array of pointers to PCB structures
struct PCB *PCBLIST[1024];

// make some PCB objects
struct PCB pcbParent;
struct PCB pcbChild1;
struct PCB pcbChild2;

int main(void)
{
   // initialize PCBLIST
   for (int i = 0;i < 1024;i++)
      PCBLIST[i] = NULL;

   // initialize parent
   pcbParent.used = 1;
   pcbParent.PID[0] = 0;
   pcbParent.Other_Resources = NULL;
   strcpy(pcbParent.type,"Parent");
   pcbParent.list = PCBLIST;
   pcbParent.parent = NULL; // no parent
   pcbParent.children = &pcbChild1;
   pcbParent.next = NULL; // no next sibling
   pcbParent.priority = 0;
   PCBLIST[0] = &pcbParent;

   // initialize child1
   pcbChild1.used = 1;
   pcbChild1.PID[0] = 1;
   pcbChild1.Other_Resources = NULL;
   strcpy(pcbChild1.type,"Child");
   pcbChild1.list = PCBLIST;
   pcbChild1.parent = &pcbParent;
   pcbChild1.children = NULL; // no children
   pcbChild1.next = &pcbChild2; // next sibling
   pcbChild1.priority = 0;
   PCBLIST[1] = &pcbChild1;

   // initialize child2
   pcbChild2.used = 1;
   pcbChild2.PID[0] = 2;
   pcbChild2.Other_Resources = NULL;
   strcpy(pcbChild2.type,"Child");
   pcbChild2.list = PCBLIST;
   pcbChild2.parent = &pcbParent;
   pcbChild2.children = NULL; // no children
   pcbChild2.next = NULL; // no next sibling
   pcbChild2.priority = 0;
   PCBLIST[2] = &pcbChild2;

   // do some stuff with the nodes

   return 0;
}
#包括//strcpy
//PCB结构定义
结构PCB
{
使用int;
char-PID[1];
结构RCB*其他资源;
字符类型[10];
结构PCB**列表;
结构PCB*父级;
结构PCB*子级;
结构PCB*下一步;
int优先级;
};
//指向PCB结构的未初始化指针数组
结构PCB*PCBLIST[1024];
//制作一些PCB对象
结构PCB父级;
结构pcbChild1;
结构pcbChild2;
内部主(空)
{
//初始化PCBLIST
对于(int i=0;i<1024;i++)
PCBLIST[i]=NULL;
//初始化父级
pcbParent.used=1;
pcbParent.PID[0]=0;
pcbParent.Other_Resources=NULL;
strcpy(pcbParent.type,“Parent”);
pcbParent.list=PCBLIST;
pcbParent.parent=NULL;//无父项
pcbParent.children=&pcbcchild1;
pcbParent.next=NULL;//没有下一个同级
pcbParent.priority=0;
PCBLIST[0]=&PCBLEST;
//初始化child1
pcbChild1.used=1;
pcbChild1.PID[0]=1;
pcbChild1.Other_Resources=NULL;
strcpy(pcbChild1.type,“Child”);
pcbChild1.list=PCBLIST;
pcbChild1.parent=&pcbcparent;
pcbChild1.children=NULL;//无子项
pcbChild1.next=&pcbChild2;//下一个兄弟姐妹
pcbChild1.priority=0;
PCBLIST[1]=&pcbChild1;
//初始化child2
pcbChild2.used=1;
pcbChild2.PID[0]=2;
pcbChild2.Other_Resources=NULL;
strcpy(pcbChild2.type,“Child”);
pcbChild2.list=PCBLIST;
pcbChild2.parent=&pcbcparent;
pcbChild2.children=NULL;//无子项
pcbChild2.next=NULL;//没有下一个同级
pcbChild2.priority=0;
PCBLIST[2]=&pcbChild2;
//对节点做一些处理
返回0;
}
这样做没有多大意义,因为你也可以直接制作一个PCB对象数组——在这种情况下,你根本不需要PCBLIST,因为列表本身就是PCB对象数组。例如,这个更有意义:

#include <string.h> // strcpy

// the PCB structure definition
struct PCB
{
   int used;
   char PID[1];
   struct RCB *Other_Resources;
   char type[10];
   struct PCB *list;
   struct PCB *parent;
   struct PCB *children;
   struct PCB *next;
   int priority;
};

// an uninitialized array of PCB structures
struct PCB PCBLIST[1024];

int main(void)
{
   // initialize PCBLIST
   for (int i = 0;i < 1024;i++)
   {
      PCBLIST[i].used = 0;
      PCBLIST[i].Other_Resources = NULL;
      PCBLIST[i].list = PCBLIST;
      PCBLIST[i].parent = NULL; // no parent by default
      PCBLIST[i].children = NULL; // no children by default
      PCBLIST[i].next = NULL; // no next sibling by default
   }

   // initialize parent
   PCBLIST[0].used = 1;
   PCBLIST[0].PID[0] = 0;
   strcpy(PCBLIST[0].type,"Parent");
   PCBLIST[0].children = &PCBLIST[1];
   PCBLIST[0].priority = 0;

   // initialize child1
   PCBLIST[1].used = 1;
   PCBLIST[1].PID[0] = 1;
   strcpy(PCBLIST[1].type,"Child");
   PCBLIST[1].parent = &PCBLIST[0];
   PCBLIST[1].next = &PCBLIST[2]; // next sibling
   PCBLIST[1].priority = 0;

   // initialize child2
   PCBLIST[2].used = 1;
   PCBLIST[2].PID[0] = 2;
   strcpy(PCBLIST[2].type,"Child");
   PCBLIST[2].parent = &PCBLIST[0];
   PCBLIST[2].priority = 0;

   // do some stuff with the nodes

   return 0;
}
#包括//strcpy
//PCB结构定义
结构PCB
{
使用int;
char-PID[1];
结构RCB*其他资源;
字符类型[10];
结构PCB*列表;
结构PCB*父级;
结构PCB*子级;
结构PCB*下一步;
int优先级;
};
//未初始化的PCB结构阵列
结构PCB PCB列表[1024];
内部主(空)
{
//初始化PCBLIST
对于(int i=0;i<1024;i++)
{
PCBLIST[i]。已使用=0;
PCBLIST[i]。其他_资源=NULL;
PCBLIST[i].list=PCBLIST;
PCBLIST[i].parent=NULL;//默认无父项
PCBLIST[i].children=NULL;//默认无子项
PCBLIST[i].next=NULL;//默认情况下没有下一个同级
}
//初始化父级
PCBLIST[0]。已使用=1;
PCBLIST[0].PID[0]=0;
strcpy(PCBLIST[0]。键入“父项”);
PCBLIST[0]。子项=&PCBLIST[1];
PCBLIST[0]。优先级=0;
//初始化child1
PCBLIST[1]。已使用=1;
PCBLIST[1].PID[0]=1;
strcpy(PCBLIST[1]。键入“Child”);
PCBLIST[1]。父项=&PCBLIST[0];
PCBLIST[1]。下一个=&PCBLIST[2];//下一个同级
PCBLIST[1]。优先级为0;
//初始化child2
PCBLIST[2]。已使用=1;
PCBLIST[2].PID[0]=2;
strcpy(PCBLIST[2]。输入“Child”);
PCBLIST[2]。父项=&PCBLIST[0];
PCBLIST[2]。优先级为0;
//对节点做一些处理
返回0;
}

是的,您需要根据一个结构的大小(其成员的总和)分配与结构数组所需内存相等的内存乘以数组的长度。我也会从一个较小的数组开始,以保持简单。在分配分配内存时也取消对指针的引用;上面,内存分配给指针,而不是指向的空间。
struct PCB*PCBLIST[1024]
内存是在堆栈上自动分配的。但是指向
结构PCBLIST
的每个指针都没有分配。您需要为
PCBLIST[index]=malloc(sizeof(PCB))分配内存;
您不必使用任何动态的东西-您可以创建静态的,就像其他答案建议的或局部变量一样