Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Structs and Malloc Posted by Aphillios on 11 May 2010 at 10:37 AM
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;
                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");
     }
}


Of course any help would be fantastic and if there's anything you need extra just ask.
Report
Re: Structs and Malloc Posted by AsmGuru62 on 11 May 2010 at 6:59 PM
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");
:      }
: }
: 
:
Report
Re: Structs and Malloc Posted by Aphillios on 11 May 2010 at 10:44 PM
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.
Report
Re: Structs and Malloc Posted by AsmGuru62 on 12 May 2010 at 12:24 AM
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:

// j001.cpp : Defines the entry point for the console application.
//

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

#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;
}

There was a few warnings, but they were not connected to strcpy().



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.