Ok thanks, fixed the memory problem. That was something I kind of just threw in there without thinking about to be honest. Can you explain why the program seems to skip the 0 index and output "Ally" on the 1st index instead?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define BUFSIZE 25
struct tag {
char *first,
*middle,
*last;
};
struct social {
struct tag name;
char *socialnum;
};
void printdata(struct social a);
int main (int argc, const char * argv[])
{
struct social fellow[5];
for (int i=0; i < 5; i++)
{
fellow[i].name.first = malloc(BUFSIZE);
fellow[i].name.middle = malloc(BUFSIZE);
fellow[i].name.last = malloc(BUFSIZE);
fellow[i].socialnum = malloc(BUFSIZE);
}
strcpy(fellow[0].name.first, "Ally");
strcpy(fellow[0].name.middle, "Amy");
strcpy(fellow[0].name.last, "Anderson");
strcpy(fellow[0].socialnum, "123456789");
strcpy(fellow[1].name.first, "Benny");
strcpy(fellow[1].name.middle, "Benjamin");
strcpy(fellow[1].name.last, "Baker");
strcpy(fellow[1].socialnum, "234567891");
strcpy(fellow[2].name.first, "Cathy");
strcpy(fellow[2].name.middle, "Cedar");
strcpy(fellow[2].name.last, "Cenders");
strcpy(fellow[2].socialnum, "345678912");
strcpy(fellow[3].name.first, "Darell");
strcpy(fellow[3].name.middle, "Dimebag");
strcpy(fellow[3].name.last, "Deezy");
strcpy(fellow[3].socialnum, "456789123");
strcpy(fellow[4].name.first, "Edward");
strcpy(fellow[4].name.middle, "Eddy");
strcpy(fellow[4].name.last, "Edision");
strcpy(fellow[4].socialnum, "567891234");
for (int i=0; i < 5; i++)
{
printdata(fellow[i]);
}
for (int i=0; i < 5; i++)
{
free(fellow[i].name.first);
free(fellow[i].name.middle);
free(fellow[i].name.last);
free(fellow[i].socialnum);
}
puts("\n");
return 0;
}
void printdata(struct social a)
{
printf("\n");
printf("%s, %s ", a.name.last, a.name.first);
if (a.name.middle)
printf("%c. ", *a.name.middle );
printf("-- %s", a.socialnum);
}