I've a problem with this code, I've to create a matrix of semaphores using the struct "tavole" but when i compile there is an error:
error: incompatible types when initializing type ‘int’ using type ‘sem_t {aka union }’ tavole matrici = {chessboard[SO_ALTEZZA][SO_BASE], sem_matrix[SO_ALTEZZA][SO_BASE], posFree};
Can you explain me why?
typedef struct {
int scacchiera[SO_ALTEZZA][SO_BASE];
sem_t semafori[SO_ALTEZZA][SO_BASE];
int posLibere;
}tavole;
int main(void){
void inizializza_matrice(int matrice[SO_ALTEZZA][SO_BASE],
int n);
void inizializza_sem_matrix(
sem_t matrice[SO_ALTEZZA][SO_BASE],
int n);
int chessboard[SO_ALTEZZA][SO_BASE], posFree = 0, value = 0;
sem_t sem_matrix[SO_ALTEZZA][SO_BASE];
tavole matrici =
{chessboard[SO_ALTEZZA][SO_BASE],
sem_matrix[SO_ALTEZZA][SO_BASE],
posFree};
inizializza_matrice(matrici.scacchiera, 0);
inizializza_sem_matrix(matrici.sem_matrix, 0);
for(int i = 0; i < SO_ALTEZZA; i++){
printf("\n");
for(int j = 0; j < SO_BASE; j++){
sem_getvalue(&matrici.semafori[i][j], &value);
printf("%d ", value);
}
}
return 0;
}
void inizializza_matrice
(int matrice[SO_ALTEZZA][SO_BASE], int n){
for(int i = 0; i < SO_ALTEZZA; i++){
for(int j = 0; j < SO_BASE; j++){
matrice[i][j] = n;
}
}
}
void inizializza_sem_matrix
(sem_t matrix[SO_ALTEZZA][SO_BASE], int n){
for(int i = 0; i < SO_ALTEZZA; i++){
for(int j = 0; j < SO_BASE; j++){
sem_init(&matrix[i][j], 0, n);
}
}
}
You can replace this:
int chessboard[SO_ALTEZZA][SO_BASE], posFree = 0, value = 0;
sem_t sem_matrix[SO_ALTEZZA][SO_BASE];
tavole matrici =
{chessboard[SO_ALTEZZA][SO_BASE],
sem_matrix[SO_ALTEZZA][SO_BASE],
posFree};
With this:
int value = 0;
tavole matrici;
You don't need to declarate the struct members before creating the struct.
There is also another problem in the code. You should replace this:
inizializza_sem_matrix(matrici.sem_matrix, 0);
With this:
inizializza_sem_matrix(matrici.semafori, 0);
Because the name of the struct member is semafori
, not sem_matrix
.