I have the following C struct and using the function getPerson(void)
that returns a pointer to my struct returns a pointer to a new struct using user input. The following code does not compile and it gives the following error:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[50];
int age;
} person;
person* getPerson(void)
{
person* newPerson = (person*)malloc(sizeof(person));
int ageInput;
char *nameInputPtr = (char*)malloc(50 * sizeof(char));
printf("Please enter your name: \n");
scanf("%s", nameInputPtr);
printf("Please enter your age: \n");
scanf("%d", &ageInput);
newPerson->name[50] = *nameInputPtr;
newPerson->age = ageInput;
return newPerson;
}
Error I get:
struct.c:22:2: error: array index 50 is past the end of the array (which contains 50 elements)
[-Werror,-Warray-bounds]
newPerson->name[50] = *nameInputPtr;
^ ~~
struct.c:6:2: note: array 'name' declared here
char name[50];
^
I manage to fix my error by the following change in the line 22:
22 newPerson->name[49] = *nameInputPtr;
So my change was to change number 50, to number 49 to be inside the bounds of the index definition of line 6.
Therefore I don't understand why line 6 and line 22 give an error in my original code and I would like to have an explanation about the error and on the clarity and functionality of my solution to this.