0

I am having some issues with pointers (I am very new to coding). In main() I am trying to initialize my employee roster through InitWorkerArray so I can then print it (that function is not included below), but whenever I try CodeBlocks just shuts down. I've looked up a lot of information on pointers, but few seem to deal with arrays/structs, so any help would be appreciated. I'm sure I have a lot of errors, unfortunately.

#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;
}
peo965
  • 39
  • 2
  • 10
  • What is the error message you're getting? What is the focus of your problem? If you're just looking for code review, check the codereview.stackexchange.com Q&A – Marco Aurélio Deleu Nov 13 '15 at 18:03
  • I think you're missing a } at the end of main. Also, you should put void in the parameter brackets of main. – Bobby Sacamano Nov 13 '15 at 18:03
  • 3
    Your setting pRoster to a malloc block, then bizarrely setting it on the very next line to NULL. That is not going to do anything useful! – Richard Nov 13 '15 at 18:05
  • Don't retype program into the question, instead copy and paste the actual code. That will make sure that you don't introduce any other errors that are irrelevant to the problem you're actually having. In your case, there's no end of the `main` function, and the code you show is not possible to build. – Some programmer dude Nov 13 '15 at 18:12
  • @marco I'm not getting any error message, CodeBlocks literally just stops working – peo965 Nov 13 '15 at 18:13

2 Answers2

2

The big problem is in these two lines:

pRoster = (workerT*)malloc(sizeof(workerT) * maxRoster);
pRoster = NULL;

In the first line you allocate memory and assign it to pRoster, but then in the very next line you reassign pRoster to be a null pointers. Dereferencing the null pointer later in the initWorkerArray function will lead to undefined behavior. The most likely result of this UB is a crash.

Also, in C you should not cast the result of malloc, or any other function returning void *. This won't cause any problems if you include the correct header files, but you should still not do it.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Also, in initWorkerArray, the calls with strcpy(pList[1]...) and strcpy(pList[2]...), pList[1].pName, etc. were never allocated, so these strcpy calls will crash.

DBug
  • 2,502
  • 1
  • 12
  • 25