I've been studying some C on my own and I was wondering if my segmentation fault is do to the way I'm trying to add *person to a dynamic array named students. This originally started out as a method for me trying to learn how to use dynamic arrays but I got stumped with how I am failing to get around this scanf faulting my program. I included everything I've done, but I suspect my problem is within the main, specifically the scanf.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//ARR_LEN
#define ARR_LEN 5
#define STR_LEN 15
//STUDENT STRUCT
typedef struct{
int ID;
char *name;
}Student;
typedef struct{
Student *array;
size_t used;
size_t size;
}Array;
void initArray(Array *a, size_t initialSize){
//ALLOCATING INITIAL SPACE
a->array = (Student *)malloc(initialSize * sizeof(Student));
a->used = 0;
a->size = initialSize;
//INITIALIZES ALL VALUES OF THE ARRAY TO 0
for(unsigned int i = 0; i<initialSize; i++){
memset(&a->array[i],0,sizeof(Student));
}
}
//ADDING A STUDENT ELEMENT INTO ARRAY
void insertArray(Array *a,Student element){
if(a->used == a->size){
a->size *= 2;
a->array = (Student *)realloc(a->array,a->size * sizeof(Student));
}
//Copy Name
a->array[a->used].name = (char*)malloc(strlen(element.name) + 1);
strcpy(a->array[a->used].name, element.name);
//Copy ID
a->array[a->used].ID=element.ID;
a->used++;
}
void freeArray(Array *a){
for(int i=0;i<a->used;i++){
free(a->array[0].name);
a->array[0].name=NULL;
}
//Freeing the array
free(a->array);
a->array = NULL;
a->used = 0;
a->size = 0;
}
int main(int argc, const char * argv[]){
char choice;
Array student;
Student *person;
char* firstName;
firstName = (char *)malloc(100*sizeof(char));
int* number;
number = (int *)malloc(10*sizeof(int));
printf("Would you like to make a list of people? (y/n): ");
scanf("%c \n",&choice);
while(choice == 'y'){
initArray(&student, 5);
printf("\nPlease enter students name: ");
scanf("%s \n",&firstName);
printf("NAME ENTERED: %s\n",firstName);
// fgets(firstName,100,stdin);
printf("\nPlease enter students ID: ");
scanf("%i \n",number);
// fgets(number,100,stdin);
for(int i = 0; i<ARR_LEN;i++){
person->name = firstName;
person->ID = (int)number;
insertArray(&student,*person);
printf("Would you like to add another? (y/n): \n");
}
}
printf("Would you like to print the list of people? (y/n): ");
scanf("%c \n",&choice);
if(choice == 'y'){
for(int x=0;x<sizeof(student);x++){
printf("Name: %s", student.array[x].name);
}
}
}
Now to give credit where its due. I grabbed the overall Dynamic Array scheme from "Casablanca" and was seeing if I could use it to overall understand how the Array was built. His/Her post can be found here: C dynamically growing array