Hello, I need some help on this:
Isn't
int *array[3];
the same thing as
int *array = (int*) malloc(3*sizeof(int*)); ??
I used the first declaration to print the integers: *array[i] and it worked.
When I used the second one for N pointers, I get an error about invalid argument type of 'unary*' (have 'int').
Thank you.
Edit:
Maybe pasting the whole code it will help:
Code:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
void *myfunc(void *arg)
{
int *x = (int*) arg;
printf("O meu pid é %u, o meu tid é %u e recebi arg=%d \n", (unsigned) getpid(), (unsigned) pthread_self(), *x);
return (void*) x;
}
int main (int argc, char* argv[])
{
if (argc != 2)
{
perror("Falta de argumentos!\n");
printf("Erro %d\n", errno);
_exit(1);
}
int n = atoi(argv[1]), st, i;
pthread_t *tids = (pthread_t*) malloc(n*sizeof(pthread_t));
int *retval = (int*) malloc(n*sizeof(int*));
for(i=1; i<=n; i++)
{
st= pthread_create(&tids[i-1], NULL, myfunc, (void*) &i);
if (st != 0)
{
perror("Erro em pthread_create()\n");
printf("Erro %d\n", errno);
_exit(2);
}
}
for (i=1; i<=n; i++)
{
st = pthread_join(tids[i-1], (void**) &retval[i-1]);
if (st != 0)
{
perror ("Erro em pthread_join()\n");
printf("Erro %d\n", errno);
_exit(3);
}
printf("Thread %u devolveu %d\n", (unsigned) tids[i-1], *retval[i-1]);
}
_exit(0);
}