I can see two things:
1. The IF() statement with allocation has misplaced bracket.
2. Incorrect way of assigning one string to another string.
In general strcpy() is the only way.
Also, I do not see how loadData() function is called - with which parameters?
: Hiya, I've been trying to allocate memory and put a single letter in
: an array within a struct that I know can actually fit it in. At the
: end of the day it just gives me a Segmentation Error and I'm back to
: wondering what I'm doing wrong.
:
: Here's all the code.
:
:
:
: int loadData(PMSType* pms, char* programFile, char* courseFile)
: {
: char input[PROG_ID_LEN + 1] = "P0001";
: ProgramType *prog;
: printf("Debug: Attempting to allocate memory");
: if((prog =(ProgramType *) malloc(sizeof(ProgramType))) == NULL)
: printf("Error: Could not allocate memory");
: else{
: printf("Debug: Attempting to put p in progID[0]");
: prog->progID[0] = 'p';
: strcpy(prog->progID, "p");
: /*prog->progID = input; - not a proper way!*/
: strcpy(prog->progID, input);
: }
:
: }
:
:
: typedef struct program
: {
: char progID[PROG_ID_LEN + 1]; /* Unique ID for a program */
: char progTitle[MAX_TITLE_LEN + 1];
: char progMajor[MAX_TITLE_LEN + 1];
: char progCode[PROG_CODE_LEN + 1];
: float progDuration;
: char progType[PROG_TYPE_LEN]; /* Undergrade(UG) or Postgrad (PG) */
: char progMode[MODE_LEN]; /* Full-time (FT) or Part-time (PT) */
: char progDescription[MAX_DESC_LEN];
: char majorDescription[MAX_DESC_LEN];
: ProgramTypePtr nextProgram;
: CourseTypePtr headCourse;
: unsigned numCourses;
: } ProgramType;
:
: typedef struct pms
: {
: ProgramTypePtr headProgram;
: unsigned numPrograms;
: } PMSType;
:
: :
:
: Also, it has told me that I have to initialise the system to a safe
: empty place... and I'm wondering if this is what I am supposed to
: put.
:
:
:
: int systemInit(PMSType* pms)
: {
: pms = (PMSType *) malloc(sizeof(*pms));
: if(pms == NULL)
: printf("Error: Couldn't Allocate\n");
: else{
: printf("Successfully allocated\n");
: }
: }
: :