D
dtschoepe
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here's the parts of the code in question...
This is part of the .h file where the struct us defined...
enum color { COLORS };
typedef enum color Color;
struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
};
typedef struct person Person;
struct node
{
struct node *next;
Person client;
};
typedef struct node Node;
Now here is the function that I am having trouble with... If you look
down to the section where this line is found...
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));
The compiler stops there and generates the seg fault message. Here's
the full function. By the way I have tested the first part of the
function, the file pointer exists and the output to stdout from printf
is working ok until I get the line above. Then the program crashes.
Am I trying to allocate the memory in the wrong way?
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person *member;
if (!feof(infile))
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);
}
else
return NULL;
printf("top of createMember\n");
/* read member attributes from file */
fgets(fname, sizeof fname, infile);
fname[strlen(fname)-1] = '\0';
fgets(lname, sizeof lname, infile);
lname[strlen(lname)-1] = '\0';
fgets(sex, sizeof sex, infile);
sex[strlen(sex)-1] = '\0';
fgets(age, sizeof age, infile);
age[strlen(age)-1] ='\0';
fgets(color, sizeof color, infile);
color[strlen(color)-1] = '\0';
fgets(hobby, sizeof hobby, infile);
hobby[strlen(hobby)-1] = '\0';
printf("got through fgets statements.\n");
/* create memory allocation for member attributes */
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));
printf("calloc firstName\n");
member->lastName = (char *)malloc(sizeof(fname) * sizeof(char));
member->hobby = (char *)malloc(sizeof(hobby) * sizeof(char));
colorS = (char *)malloc(sizeof(color) * sizeof(char));
printf("got past malloc statements\n");
/* store member attributes */
strcpy(member->firstName, fname);
printf("fname processed\n");
sscanf(lname, "%s", member->lastName);
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);
strncpy(colorS, color, sizeof(color));
member->favColor = readColor(colorS);
sscanf(hobby, "%s", member->hobby);
printf("bottom of createMember\n");
return member;
}
David
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here's the parts of the code in question...
This is part of the .h file where the struct us defined...
enum color { COLORS };
typedef enum color Color;
struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
};
typedef struct person Person;
struct node
{
struct node *next;
Person client;
};
typedef struct node Node;
Now here is the function that I am having trouble with... If you look
down to the section where this line is found...
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));
The compiler stops there and generates the seg fault message. Here's
the full function. By the way I have tested the first part of the
function, the file pointer exists and the output to stdout from printf
is working ok until I get the line above. Then the program crashes.
Am I trying to allocate the memory in the wrong way?
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person *member;
if (!feof(infile))
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);
}
else
return NULL;
printf("top of createMember\n");
/* read member attributes from file */
fgets(fname, sizeof fname, infile);
fname[strlen(fname)-1] = '\0';
fgets(lname, sizeof lname, infile);
lname[strlen(lname)-1] = '\0';
fgets(sex, sizeof sex, infile);
sex[strlen(sex)-1] = '\0';
fgets(age, sizeof age, infile);
age[strlen(age)-1] ='\0';
fgets(color, sizeof color, infile);
color[strlen(color)-1] = '\0';
fgets(hobby, sizeof hobby, infile);
hobby[strlen(hobby)-1] = '\0';
printf("got through fgets statements.\n");
/* create memory allocation for member attributes */
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));
printf("calloc firstName\n");
member->lastName = (char *)malloc(sizeof(fname) * sizeof(char));
member->hobby = (char *)malloc(sizeof(hobby) * sizeof(char));
colorS = (char *)malloc(sizeof(color) * sizeof(char));
printf("got past malloc statements\n");
/* store member attributes */
strcpy(member->firstName, fname);
printf("fname processed\n");
sscanf(lname, "%s", member->lastName);
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);
strncpy(colorS, color, sizeof(color));
member->favColor = readColor(colorS);
sscanf(hobby, "%s", member->hobby);
printf("bottom of createMember\n");
return member;
}
David