Structs and Malloc - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Structs and Malloc

AphilliosAphillios Posts: 27Member
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.

[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;
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;

[/code]

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.

[code]
int systemInit(PMSType* pms)
{
pms = (PMSType *) malloc(sizeof(*pms));
if(pms == NULL)
printf("Error: Couldn't Allocate
");
else{
printf("Successfully allocated
");
}
}
[/code]

Of course any help would be fantastic and if there's anything you need extra just ask.

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]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?
    [/color]
    : 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.
    :
    : [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))[color=Red])[/color] == 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");
    : [color=Red]/*prog->progID = input; - not a proper way!*/[/color]
    : 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;
    :
    : [/code]:
    :
    : 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.
    :
    : [code]:
    : int systemInit(PMSType* pms)
    : {
    : pms = (PMSType *) malloc(sizeof(*pms));
    : if(pms == NULL)
    : printf("Error: Couldn't Allocate
    ");
    : else{
    : printf("Successfully allocated
    ");
    : }
    : }
    : [/code]:
  • AphilliosAphillios Posts: 27Member
    LoadData() is called in the main function with pms, and two arguments from the command line, I felt that it wasn't necessary to add that as I'm just wondering whether I am doing anything wrong with malloc and adding to an array of chars in a struct.

    Everytime I try that strcpy(prog->progID, input); or indeed strcpy(prog->progID, "p"); it gives me an error of a mismatched type between Char[6] and Char * or Char (*) [6] and I have no idea what to do about that.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Your compiler is at fault here. Which compiler is it?
    I just tried it with Visual C++ 2005 and it all compiles fine.
    I adjusted the code, so it will compile as pure C code:[/color]
    [code]
    // j001.cpp : Defines the entry point for the console application.
    //

    #include
    #include
    #include

    #define PROG_ID_LEN 32
    #define MAX_TITLE_LEN 32
    #define PROG_CODE_LEN 32
    #define PROG_TYPE_LEN 32
    #define MODE_LEN 32
    #define MAX_DESC_LEN 32

    typedef struct tag_ProgramType
    {
    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];
    struct tag_ProgramType* nextProgram;
    //CourseType* headCourse;
    unsigned numCourses;
    } ProgramType;

    typedef struct pms
    {
    ProgramType* headProgram;
    unsigned numPrograms;
    } PMSType;

    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");
    strcpy(prog->progID, input);
    }
    return 0;
    }

    int main(int argc, char* argv[])
    {
    return 0;
    }
    [/code]
    [color=Blue]There was a few warnings, but they were not connected to strcpy().[/color]
Sign In or Register to comment.