Hey there.
I am doing some c program that simulate a call center using threads. In the print function it gives me this error: aggregate value used where an ineteger was expecter. I already tried to make a cast to int but it still give me this error.

The variable var_telefonista was declared global like an sem_t.

void *print(void *nume)
{
  
  while(1){
    pthread_mutex_lock(&mutex);
    int x =  (int)var_telefonista;//total number of call operators
    printf("Estão a decorrer %d chamadas\n",var_telefonista);
    printf("O número de operadores livres é %d\n",((int)nume - x));// ->ERROR is here!!
    printf("Chamadas em espera %d\n",espera);
    pthread_mutex_unlock(&mutex);
    sleep(5);
  }

If some know any solution plz tell me.
Thanks

Which line are you getting that error on?

The error is signed in the line that i write ERROR is here.

OP> In the print function it gives me this error: aggregate value used where an ineteger was expecter.
Did you get why?

printf("O número de operadores livres é %d\n",((int)nume - x))

OP> I already tried to make a cast to int but it still give me this error.
Casting is not a magic data converter.

printf("O número de operadores livres é %d\n",((int)nume - x))

nume is the address of a void pointer. If you need an integer make the parameter in the function to be an integer.

void *print(int *nume)

If not you need to use another pointer inside the function.

void *print(void *nume)
{
     int *n = nume;
     printf("O número de operadores livres é %d\n", (*n - x));
}

Which it would not make any sense.

The error is signed in the line that i write ERROR is here.

Well Duuuah :)

This will also work -- assuming nume is really an int pointer. If its not an int pointer than this will not work. printf("O número de operadores livres é %d\n",(*(int *)nume - x))

Thanks for the help and the explanation.
Well i tried yur solutions, but i think the problem is in my variable var_telefonista when i tried to cast sem_t to int.

I tried the following:

52  void *print(void *nume)
53  {
54  
55    while(1){
56      pthread_mutex_lock(&mutex);
57      int x =  (int)var_telefonista;
58      printf("Estão a decorrer %d chamadas\n", var_telefonista);
59      printf("O número de operadores livres é %d\n", (*(int *)nume - x));
60      printf("Chamadas em espera %d\n",espera);
61      pthread_mutex_unlock(&mutex);
62      sleep(5);
63   }
64  }

The errors are:
57 error: aggregate value used where an integer was expected
59 warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘sem_t’

how can i cast that sem_t variable to int for i can print that operation?

must be some other problem. This compiles for me. Also note that you do not have to manually add the line numbers -- [code=c] ... [/code] will do that.

int n = 0;
void *var_telefonista = (void *)&n;

 void *print(void *nume)
 {
 
   while(1){
//     pthread_mutex_lock(&mutex);
     int x =  *(int*)var_telefonista;
     printf("Estão a decorrer %d chamadas\n", *(int*)var_telefonista);
     printf("O número de operadores livres é %d\n", (*(int *)nume - x));
//     printf("Chamadas em espera %d\n",espera);
//     pthread_mutex_unlock(&mutex);
//     sleep(5);
  }
 }

 int main()
 {

 }

Well..soo dunno why.I compiled and it give me that error i mention before.
I have like this:

sem_t var_telefonista; //this is declared in my callcenter.h

void *print(void *nume)
{

  while(1){
    pthread_mutex_lock(&mutex);
    int x =  (int)var_telefonista;
    printf("Estão a decorrer %d chamadas\n", var_telefonista);
    printf("O número de operadores livres é %d\n", (*(int *)nume - x));
    printf("Chamadas em espera %d\n",espera);
    pthread_mutex_unlock(&mutex);
    sleep(5);
  }
}

int main(int argc, char *argv[])
{
  int nrTel,nrUte,i;

  nrTel=atoi(argv[1]);
  nrUte=atoi(argv[2]);

......

  pthread_create(&printbufos,NULL,imprime_estado,(void*)nrTel);

.....

}

If yu want i can post all code, maybe yu can figured out whats the problem.
My head is getting the limit :P

what is sem_t ? Probably a typedef of an int. You should be able to typecase it. printf("Estão a decorrer %d chamadas\n", (int)var_telefonista);

sem_t is part of the POSIX version of semaphore threads.
basically each semaphore is represented by a sem_t variable then initialized by sem_init passing the pointer back to sem_t.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.