C
Chris Potter
Hello everyone. I have two questions both of which regard a homework
assignment for my "Intro to C" class. The First question that i have
is that my program segfaults when i free() memory that i malloc'd and
i'm not sure why. (without free()ing the program operates as i would
expect it to) Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 75
#define MAX_NAMES 3
#define NAME_SIZE 25
struct Name {
char name[NAME_SIZE];
struct Name *next;
};
typedef struct Name NAME;
int main (void)
{
char *entry;
int pos = 0;
int count = 0;
int inword = 0;
NAME first, middle, last, *start, *list;
/* allocate space for *entry */
entry = malloc (MAX * sizeof (char));
if (entry == NULL ) {
printf ("\n malloc() failed!\n");
exit (1);
}
/* setup list */
start = &first;
first.next = &middle;
middle.next = &last;
last.next = NULL;
list = start;
/* null first elements in case user doesn't give 3 names */
first.name[0] = '\0';
middle.name[0] = '\0';
last.name[0] = '\0';
/* input to get first, middle and last name */
printf ("Please enter your full name: ");
fgets (entry, MAX, stdin);
/* split entry up into separate name fields */
while ((*entry) && (count < MAX_NAMES)) {
if (inword) {
if (isspace (*entry)) {
list->name[pos] = '\0';
list = list->next;
inword = 0;
pos = 0;
++count;
} else
list->name[pos++] = *entry;
} else
if (!isspace (*entry)) {
list->name[pos++] = *entry;
inword = 1;
}
++entry;
}
free (entry);
/* output list structure */
list = start;
printf ("Name in list structure is: ");
while (list != NULL) {
printf ("%s ", list->name);
list = list->next;
}
printf ("\n");
return 0;
}
I'm not neccesarily looking for an "answer", maybe just a nudge in the
direction of what the problem could be, and then i could root it out.
The second question is regarding the code where i put '\0' characters
into the first elements of the arrays in the NAME structures. I wanted
to just initialize the first element in the "name" array in the NAME
structure where the stuct was declared but the compiler wouldn't
accept that. Thanks in advance for any and all comment/tips/advice.
-Chris Potter
assignment for my "Intro to C" class. The First question that i have
is that my program segfaults when i free() memory that i malloc'd and
i'm not sure why. (without free()ing the program operates as i would
expect it to) Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 75
#define MAX_NAMES 3
#define NAME_SIZE 25
struct Name {
char name[NAME_SIZE];
struct Name *next;
};
typedef struct Name NAME;
int main (void)
{
char *entry;
int pos = 0;
int count = 0;
int inword = 0;
NAME first, middle, last, *start, *list;
/* allocate space for *entry */
entry = malloc (MAX * sizeof (char));
if (entry == NULL ) {
printf ("\n malloc() failed!\n");
exit (1);
}
/* setup list */
start = &first;
first.next = &middle;
middle.next = &last;
last.next = NULL;
list = start;
/* null first elements in case user doesn't give 3 names */
first.name[0] = '\0';
middle.name[0] = '\0';
last.name[0] = '\0';
/* input to get first, middle and last name */
printf ("Please enter your full name: ");
fgets (entry, MAX, stdin);
/* split entry up into separate name fields */
while ((*entry) && (count < MAX_NAMES)) {
if (inword) {
if (isspace (*entry)) {
list->name[pos] = '\0';
list = list->next;
inword = 0;
pos = 0;
++count;
} else
list->name[pos++] = *entry;
} else
if (!isspace (*entry)) {
list->name[pos++] = *entry;
inword = 1;
}
++entry;
}
free (entry);
/* output list structure */
list = start;
printf ("Name in list structure is: ");
while (list != NULL) {
printf ("%s ", list->name);
list = list->next;
}
printf ("\n");
return 0;
}
I'm not neccesarily looking for an "answer", maybe just a nudge in the
direction of what the problem could be, and then i could root it out.
The second question is regarding the code where i put '\0' characters
into the first elements of the arrays in the NAME structures. I wanted
to just initialize the first element in the "name" array in the NAME
structure where the stuct was declared but the compiler wouldn't
accept that. Thanks in advance for any and all comment/tips/advice.
-Chris Potter