In the thread programming environment on the C language, we have to create a "struct" data structure if more than one arguments are needed. I need a two dimensional array on that struct. I got a syntax error when I declare it! The following is the code:

#include<stdio.h> 
#include "mpi.h" 
#include<pthread.h> 
#define MAXTHREADS 2 
#define igg 5    //303 
#define jgg 3    //1233 
 
 
 struct thread_data{ 
          int r_s; 
          int r_e; 
      int rnk; 
          int a[igg][jgg]; 
        }; 
 
void *mat_mul(void * thread_arg) 
{ 
   int row, start_row, end_row, j; 
   struct thread_data* my_data; 
   my_data=(struct thread_data*) thread_arg; 
   start_row=my_data->r_s; 
   end_row=my_data->r_e; 
   for(row=start_row; row<end_row; row++) 
     for(j=0; j<jgg; j++) 
        my_data->a[row][j]=my_data->a[row][j]*my_data->rnk; 
   pthread_exit((void *)0); 
} 
 struct thread_data tda[MAXTHREADS]; 
main(int argc, char **argv) 
{ 
 
 
    int rank, size, tag, rc, i,j, le; 
    MPI_Status status; 
    char message[20]; 
    char pname[20]; 
    int a[igg][jgg]; 
    void sum(int, int[][]); 
 
     //struct thread_data tda[MAXTHREADS]; 
      //pthread_t tid[MAXTHREADS]; 
 
    rc=MPI_Init(&argc, &argv); 
    rc=MPI_Comm_size(MPI_COMM_WORLD, &size); 
    rc=MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    rc=MPI_Get_processor_name(pname, &le); 
        for(i=0; i<igg; i++) 
      for(j=0; j<jgg; j++) 
      a[i][j]=i+j; 
    sum(rank, a); 
        for(i=0; i<igg; i++) 
    { 
         for(j=0; j<jgg; j++) 
             printf(" %d ", a[i][j]); 
         printf("\n"); 
     } 
    rc=MPI_Finalize(); 
} 
     
void sum(int rnk, int x[][]) 
{ 
   int s, i, j, trd_size, t; 
 
   struct thread_data tda[MAXTHREADS]; 
       pthread_t tid[MAXTHREADS]; 
      trd_size=2; 
     tda[0].r_s=0; 
     tda[0].r_e=igg/2; 
     tda[0].rnk=rnk; 
     
     tda[1].r_s=igg/2; 
     tda[1].r_e=igg; 
     tda[1].rnk=rnk; 
   
    for(i=0; i<igg/2; i++) 
        for(j=0; j<jgg; j++) 
            tda[0].a[i][j]=x[i][j]; 
    for(i=igg/2; i<igg; i++) 
        for(j=0; j<jgg; j++) 
                  tda[1].a[i][j]=x[i][j]; 
 
        for(t=0; t<trd_size; t++) 
            if(pthread_create(&tid[t], NULL, mat_mul, (void *) &tda[t])!=0) 
                perror("pthread_create() failed.\n\n"); 
 
        for(t=0; t<trd_size; t++) 
            if(pthread_join(tid[t], NULL)!=0) 
                perror("pthread_join() failed.\n\n"); 
    for(i=0; i<igg/2; i++) 
        for(j=0; j<jgg; j++) 
                  x[i][j]=tda[0].a[i][j]; 
    for(i=igg/2; i<igg; i++) 
        for(j=0; j<jgg; j++) 
            x[i][j]=tda[1].a[i][j]; 
}

The following is the syntax error:
[ganesh@upplanka ~]$ mpicc -lm -lpthread -o thread_eg thread_eg.c
thread_eg.c: In function ‘main’:
thread_eg.c:37: error: array type has incomplete element type
thread_eg.c:48: error: type of formal parameter 2 is incomplete
thread_eg.c: At top level:
thread_eg.c:58: error: array type has incomplete element type
[ganesh@upplanka ~]$

Recommended Answers

All 4 Replies

You can't take a 2D array as a function argument with both diamentions of variable length (it's allowed for single diamention but not for more than one !)
i.e.

void my_function1( int arr_1D[] ) {/*...*/} //This is allowed
void my_function2( int arr_2D[][] ) {/*...*/} //This is NOT allowed

make it: void sum(int, int[igg][jgg]); and it should work

commented: Great advice -Aia +1

make it: void sum(int, int[igg][jgg]); and it should work

This would work also:

void sum(int, int[][jgg]);

Or, you could just pass it as a pointer. And either way, you'll want to pass some sizes with it to make sure you stay within your allocated memory.

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.