在C中初始化指向数组的指针

在C中初始化指向数组的指针,c,arrays,pointers,struct,initialization,C,Arrays,Pointers,Struct,Initialization,我对指针有一些问题(我对编码非常陌生)。在main()中,我试图通过initworkerary初始化我的员工花名册,这样我就可以打印它(下面不包括该函数),但每当我尝试代码块时,代码块就会关闭。我查阅了大量有关指针的信息,但似乎很少有人涉及数组/结构,因此,如果有任何帮助,我将不胜感激。不幸的是,我肯定我犯了很多错误 #include <stdio.h> #include <stdlib.h> typedef struct workerT_struct { ch

我对指针有一些问题(我对编码非常陌生)。在main()中,我试图通过
initworkerary
初始化我的员工花名册,这样我就可以打印它(下面不包括该函数),但每当我尝试代码块时,代码块就会关闭。我查阅了大量有关指针的信息,但似乎很少有人涉及数组/结构,因此,如果有任何帮助,我将不胜感激。不幸的是,我肯定我犯了很多错误

#include <stdio.h>
#include <stdlib.h>

typedef struct workerT_struct {
    char *pName; //employee name
    int maxName; //size, in chars, of the memory buffer to store emp name
    char *pTitle; //employee title
    int maxTitle; //size of the memory buffer created to store title
} workerT;
void initWorkerArray(workerT *pList, int siz);
void prntWorker(workerT *pList, int siz, int indx); 

int main()
{
    int maxRoster = 8;
    workerT *pRoster;
    pRoster = (workerT*)malloc(sizeof(workerT) * maxRoster);
    pRoster = NULL;
    initWorkerArray(pRoster, maxRoster);
return 0;
}
void initWorkerArray(workerT *pList, int siz) {

    pList[0].maxName = 32; 
    pList[0].maxTitle = 50;
    pList[0].pName = malloc(sizeof(char) * maxName);
    pList[0].pTitle = malloc(sizeof(char) * maxTitle);

    strcpy(pList[0].pName, "Buggy, Orson");
    strcpy(pList[0].pTitle, "Director/President");

    strcpy(pList[1].pName, "Czechs, Imelda");
    strcpy(pList[1].pTitle, "Chief Financial Officer");

    strcpy(pList[2].pName, "Hold, Levon");
    strcpy(pList[2].pTitle, "Customer Service");
    return;
}
void prntWorker(workerT *pList, int siz, int indx) {
    int i = indx;
        printf("%s, ", pList[i].pName);
        printf("%s, ", pList[i].pTitle);
        printf("\n\n");
    return;
}
#包括
#包括
typedef结构workerT_结构{
char*pName;//员工姓名
int maxName;//存储emp名称的内存缓冲区大小(以字符为单位)
char*pTitle;//员工职务
int maxTitle;//为存储标题而创建的内存缓冲区的大小
}沃克特;
void initworkerary(workerT*pList,整数大小);
无效prntWorker(workerT*pList、int siz、int indx);
int main()
{
int-maxfloster=8;
沃克特*普罗斯特;
pRoster=(workerT*)malloc(sizeof(workerT)*MaxFloster);
pRoster=NULL;
初始工作阵列(pRoster、MaxFloster);
返回0;
}
void initworkerary(workerT*pList,整数大小){
pList[0].maxName=32;
pList[0]。maxTitle=50;
pList[0].pName=malloc(sizeof(char)*maxName);
pList[0].pTitle=malloc(sizeof(char)*maxtile);
strcpy(pList[0].pName,“Buggy,Orson”);
strcpy(pList[0].pTitle,“董事/总裁”);
strcpy(pList[1].pName,“Czechs,Imelda”);
strcpy(pList[1].pTitle,“首席财务官”);
strcpy(pList[2].pName,“Hold,Levon”);
strcpy(pList[2].pTitle,“客户服务”);
返回;
}
无效prntWorker(workerT*pList、int siz、int indx){
int i=indx;
printf(“%s”,pList[i].pName);
printf(“%s”,pList[i].pTitle);
printf(“\n\n”);
返回;
}

最大的问题在于这两行:

pRoster = (workerT*)malloc(sizeof(workerT) * maxRoster);
pRoster = NULL;
在第一行中,您分配内存并将其分配给
pRoster
,但在下一行中,您将
pRoster
重新分配为空指针。稍后在
initworkerary
函数中取消对空指针的引用将导致错误。最有可能的结果是崩溃


另外,也可以是返回
void*
的任何其他函数。如果包含正确的头文件,这不会导致任何问题,但仍然不应该这样做。

最大的问题在于这两行:

pRoster = (workerT*)malloc(sizeof(workerT) * maxRoster);
pRoster = NULL;
在第一行中,您分配内存并将其分配给
pRoster
,但在下一行中,您将
pRoster
重新分配为空指针。稍后在
initworkerary
函数中取消对空指针的引用将导致错误。最有可能的结果是崩溃


另外,也可以是返回
void*
的任何其他函数。如果包含正确的头文件,这不会导致任何问题,但您仍然不应该这样做。

此外,在InitWorkerray中,使用strcpy(pList[1]…)和strcpy(pList[2]…)、pList[1].pName等的调用从未分配,因此这些strcpy调用将崩溃。

此外,在InitWorkerray中,使用strcpy(pList[1]…)和strcpy的调用也将崩溃(pList[2]…),pList[1].pName等从未分配过,因此这些strcpy调用将崩溃。

您收到的错误消息是什么?问题的焦点是什么?如果您只是想查看代码审阅,请查看codereview.stackexchange.com Q&AI,您认为main结尾缺少一个}。此外,您应该在main.settin的参数括号中输入voidg复制到malloc块,然后奇怪地在下一行将其设置为NULL。这不会有任何用处!不要在问题中重新键入程序,而是复制并粘贴实际代码。这将确保不会引入与实际问题无关的任何其他错误。在您的情况下,有
main
函数没有结束,并且您显示的代码无法生成。@marco我没有收到任何错误消息,代码块完全停止工作。您收到的错误消息是什么?您的问题的焦点是什么?如果您只是想查看代码审阅,请查看codereview.stackexchange.com Q&AI是否认为您丢失了ng a}在main的末尾。另外,您应该在main的参数括号中放入void。您将pRoster设置为malloc块,然后奇怪地在下一行将其设置为NULL。这不会有任何用处!不要在问题中重新键入程序,而是复制并粘贴实际代码。这将确保您不会引入与您实际遇到的问题无关的任何其他错误。在您的情况下,
main
函数没有结尾,您显示的代码无法生成。@marco我没有收到任何错误消息,代码块实际上停止工作