I'm trying to pass a 2D array to a function using pointers. The function is going to initialise the array.
I get a segmentation fault: 11 at run time
The code is as follows
typedef struct {
char name[100];
int runs;
int batstatus;
float overs;
int bruns;
int wickets;
}player;
player selectbowler(player *team) {
srand((unsigned) time(null));
int d = rand()%10;
if(team[d].overs < 4) {
player p = team[d];
return p;
}
else {
return selectbowler(team);
}
}
void updatebowlwickets(player *team[], player bowler) {
for (int i = 0; i < 10; i++)
{
//Segmentation fault was being generated over here.
if( strcmp(team[i]->name, bowler.name) == 0) {
team[i]->wickets += 1;
}
}
}
//made a call to the above method via
player teams[2][10];
//Code part goes here
player bowler = selectbowler(teams[0]);
updatebowlwickets((player **)teams[0], bowler);
I'm sorry for uploading a wrong code earlier.
Besides can you please let me know when there would be a segmentation fault: 11, I already know that a segmentation fault would be generated she we try to access an invalid address or when we run out of the memory space allocated in the hardware by the OS. Please let me know if there are any other reasons or even if the stated reasons are wrong.
Thanks!