Trying to initialize a function so its data members are 0 for the numerical values and "none" for the string but a segmentation error occurs for the strcpy.
This is for a code that creates a 2d array of an island of x,y points and prior to the later functionality I need to initialize the function to default (0's and none). I have tried to mess with the pointers and not using strcpy but to my understanding for updating strings, strcpy is needed.
typedef struct Hill {
char name[20];
int loc[2];
double height;
double slope;
} Hill;
Hill* setHill (Hill* hill){
strcpy(hill->name, "none"); // error appears to be here
hill->loc[0] = 0;
hill->loc[1] = 0;
hill->height = 0;
hill->slope = 0;
return hill;
}
int main() {
Hill* hillsArray[5];
int i;
// calling setHill to populate an array of 5 hills
for(i=0; i<5; ++i) {
setHill(hillsArray[i]);
}
// updating hill "ada's apex"
strcpy((hillsArray[0]->name), "Ada's Apex");
hillsArray[0]->loc[0] = 12;
hillsArray[0]->loc[1] = 9;
hillsArray[0]->height = 20;
hillsArray[0]->slope = 0.25;
// updating hill turing's top
strcpy((hillsArray[1]->name), "Turing's top");
hillsArray[1]->loc[0] = 4;
hillsArray[1]->loc[1] = 3;
hillsArray[1]->height = 20;
hillsArray[1]->slope = 0.33;
This updating of the hill repeats 3 more times for a total of 5 hills but its the same code just updating each hill with different values.