Domanda

This is a Matrix multiplication code. It creates a thread to multiply each row of the first matrix to the second matrix and saves the result in matrix C. It gives an error in the pthread_create line expected primary-expression before 'void'.
I run this code on ubunto 13.10 virtual machine.
Thanks in advance.

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

struct matrices
{
    int matrixA[10][10];
    int matrixB[10][10];
    int matricC[10][10];
    int r1,r2,c1,c2;
}*ptr;
int p;
void *matrixMul(void *);

int main()
{
    int i=0,j=0;
    pthread_t threads[10];
    ptr=(struct matrices*)malloc(sizeof(struct matrices));
    printf("Enter size of first matrix(Rows then Columns)");
    scanf("%d",&(ptr->r1));
    scanf("%d",&(ptr->c1));
    printf("Enter elements of first array : ");
    for(i=0; i<ptr->r1; i++)
    {
        for(j=0; j<ptr->c1; j++)
        {
            scanf("%d",&ptr->matrixA[i][j]);

        }
    }
    printf("Enter size of second matrix(Rows then Columns)");
    scanf("%d",&(ptr->r2));
    scanf("%d",&(ptr->c2));
    if(ptr->c1!=ptr->r2)
    {
        printf("Dimensions ERRORR! ");
    }
    else
    {
        printf("Enter elements of second array : ");
        for(i=0; i<ptr->r2; i++)
        {
            for(j=0; j<ptr->c2; j++)
            {
                scanf("%d",&ptr->matrixB[i][j]);

            }
        }
        for(i=0;i<ptr->r1;i++)
        {
            for(j=0;j<ptr->c2;j++)
            {
                ptr->matricC[i][j]=0;
            }
        }
        for (p=0;p<ptr->r1;p++)
        {
            **********pthread_create(&threads[p],NULL, *matrixMul,void &p);**********
        }
        for(i=0;i<ptr->r1;i++)
        {
            pthread_join(threads[i],NULL);
        }
        for(i=0;i<ptr->r1;i++)
        {
            for(j=0;j<ptr->c2;j++)
            {
                printf("%d",ptr->matricC[i][j]);
            }
        }
    }
    return 0;
}
void *matrixMul(void *rownum)
{
    int *i;
    int n=0,m=0;
    i=(int*)rownum;
    for(n=0;n<ptr->c2;n++)
    {
        for(m=0;m<ptr->c1;m++)
        {
            ptr->matricC[*i][n]+=(ptr->matrixA[*i][m])*(ptr->matrixB[m][n]);
        }
    }
    return NULL;
}
È stato utile?

Soluzione

Your code contains minor error, but the logic is correct, don't worry.
I downloaded the code and tested it on my machine, so please note the following:

This line should be written this way...

pthread_create(&threads[i],NULL, matrixMul, &i);

Because according to the specs of pthread library that pthread_create should take void pointer to the runner function and void pointer to the parameter. You don't need to add (void *) because you already declared your runner function matrixMul as void *.
Your primary error here was (void) &i and it should be &i only as you already delcaired this parameter as void * in the runner function's prototype. You shall too pass the runner function like this &matrixMul.

Some other notes: "Code Review"

  1. You shouldn't put your logic in the else statement, you can simply after printf("Dimensions ERRORR! "); write exit(-1); because this is basically what you do if the dimensions error.
  2. Check for the return value (status) of pthread_create and pthread_join
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top