I'm creating a matrix of dimension [10][10], that stores a struct Contentor,I can't change the values inside the matrix nor store them.

I try to check if there is any data with a for loop but can't see what's inside.

When I store data inside the matrix I try to create a thread of each one of them to process the data, but I don't really know how to do it...

I have tried using malloc and calloc, I create the matrix in the rudimentary form but nothing seems to work...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

struct Contentor {

    char nSerie[36];
    char porto[4];

};

void* direcionaParaDestino(void* arg) {
    struct Contentor *arg_struct = (struct Contentor*) arg;

    char destino[4] = arg_struct->porto;

    if(strcmp(destino, "ANR")) {

    } else if (strcmp(destino, "BUS")) {

    } else if (strcmp(destino, "DXB")) {

    } else if (strcmp(destino, "GUA")) {

    } else if (strcmp(destino, "HAM")) {

    } else if (strcmp(destino, "HKG")) {

    } else if (strcmp(destino, "LAX")) {

    } else if (strcmp(destino, "RTM")) {

    } else if (strcmp(destino, "SHA")) {

    } else if (strcmp(destino, "SIN")) {

    }

    pthread_exit(0);
}


int main() {
    struct Contentor parque[10][10] = malloc(100*sizeof(struct Contentor));


    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            parque[i][j].nSerie = "";
            parque[i][j].porto = "";
        }
    }


    pthread_t tids[10][10];
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            if(strcmp(parque[i][j].nSerie,"")) {
                printf("Lugar [%d][%d] encontra-se Livre\n", i, j);
                //Criar Thread que espera ate 5 segundos
                                short randomNumber = rand() % 5
                pthread_attr_t attr;
                        pthread_attr_init(&attr);
                        pthread_create(&tids[i][j], &attr, direcionaParaDestino, 1 );
                                sleep(randomNumber);

            } else {
                printf("Lugar [%d][%d] encontra-se Ocupado\n", i, j);

            }
        }
    }
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            pthread_join(tids[i][j],NULL);
        }
    }

}

I want it to be a matrix of that struct, check if there is any data in each spot, then store it and create a thread for each variable stored that will wait up to 5 secs before processing the information.

1

There are 1 answers

1
Some programmer dude On BEST ANSWER

You can't initialize arrays from other arrays or pointers. And neither can you assign to arrays.

So

char destino[4] = arg_struct->porto;

is invalid.

There are two ways to solve this problem for you:

  1. Rely on the fact that arrays naturally decay to pointers to their first element. That is, if a pointer is expected then arg_struct->porto is equal to &arg_struct->porto[0]. That means you can do

    char *destino = arg_struct->porto;
    
  2. Not have any destino variable at all, and pass arg_struct->porto directly to your strcmp calls:

    if (strcmp(arg_struct->porto, "BUS") == 0) ...
    

Also note that strcmp returns zero if the two strings are equal. That's the reason for the equality comparison against zero above.


The inability to assign to an array is also why statements like

parque[i][j].nSerie = "";

will fail.

If you want the arrays to act like empty strings, all you need to do is set the first element to the null terminator:

parque[i][j].nSerie[0] = '\0';  // Make string "empty"

If you want to clear all bytes of the array, you need to use the memset function:

memset(parque[i][j].nSerie, 0, sizeof parque[i][j].nSerie);  // "Clear" array