0

my problem today is that I need to allocate a dynamic array of structs. I have coded my entire program, but I am getting seg faults and I believe it is because I have not allocated things properly. Overall I would just like some more information on how to do this, and where I went wrong. Thanks!

Here is my struct:

struct names {
    char *Name1;
    char *Name2;
};
struct names **namePointer = NULL;

Here is where I want to allocate for the number of structs in the array where numOfElems is a variable which will count how many iterations I need. Currently it is set to 1 and will grow one each time I need more space:

numOfElems = 1;
*namePointer = realloc(*namePointer, sizeof( struct names) * numOfElems);

Here is how I want to get input, and then store the values for the structs in the array. The string was allocated before, and then reallocated for the amount of characters as this was a specification:

printf("Enter Name1: ");
fgets(namePointer[i]->Name1, 50, stdin);
stringLen = strlen(namePointer[i]->Name1) + 1;
namePointer[i]->Name1 = realloc(namePointer[i]->Name1, sizeof(char) * stringLen);

At the end of the loop there is a "i++" and then it goes through the whole process again as to hopefully submit the next values into the next spot in the struct array. I get a seg fault and am unsure of why. If there is an easier way to dynamically allocate an array of structs please share!

Thanks again.

3
  • 1
    Don't tell us what you think the code does, show it to us. We need to see your allocation of the strings and your loop. Also, where is the seg fault? Running this just once in a debugger should point you right to the offending line of code. Commented Feb 7, 2017 at 23:40
  • 1
    why do you need a struct names** type? Commented Feb 7, 2017 at 23:46
  • Your variable namePointer should be of type struct names *namePointer; with one star, not two. If you have an array of int, you use int *ptrIntArray; — similarly with an array of struct names. As written, you have allocated more memory than needed in the array of pointers to structures, but you've not allocated anything for the pointers to point at. Commented Feb 8, 2017 at 1:28

1 Answer 1

1

You need space to store the input:

printf("Enter Name1: ");
fgets(namePointer[i]->Name1, 50, stdin); /* Name1 is an uninitialized pointer  */
stringLen = strlen(namePointer[i]->Name1) + 1;
namePointer[i]->Name1 = realloc(namePointer[i]->Name1, sizeof(char) * stringLen);

Use an intermediate array and strcpy:

char temp[50];
printf("Enter Name1: ");
fgets(temp, sizeof temp, stdin);
stringLen = strlen(temp) + 1;
namePointer[i]->Name1 = realloc(namePointer[i]->Name1, sizeof(char) * stringLen);
strcpy(namePointer[i]->Name1, temp);

Or use strdup (non standard but available on many implementations):

char temp[50];
printf("Enter Name1: ");
fgets(temp, sizeof temp, stdin);
namePointer[i]->Name1 = strdup(temp);
Sign up to request clarification or add additional context in comments.

1 Comment

or namePointer[i]->Name1 = strdup(temp); instead of last 3 lines