0

I have the below:

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

void srand48(long int seedval);
double drand48(void);

typedef struct {
  int mtxsize;
  int threadID;
  double *(*a)[];
  double *b;
} mtxargs;

mtxargs *newArgs (int n, int id, double *(*a)[], double *b);

int main(int argc, char *argv[]) {
  int   n=4;    // problenm size
  int   seed=10;// seed for srand48() / drand48()
  int   itt_max=5;// number of itterations to preform
  int   itt;    // current itteration 
  int   i, j;   // indices into arrays
  double    sum;    // computes the inner products for A * t
  double    error;  // max | t1[i] - t[i] |
  double    errori; // | t1[i] - t[i] |
  char  ch; // for error checking on command line args.
  double *ptrB;

  pthread_t thread1,thread2,thread3,thread4;
  mtxargs *a1, *a2, *a3, *a4;

  if( argc == 4 ) {
    if( (sscanf(argv[1],"%d %[^ /t]", &n, &ch) != 1) ||
        (sscanf(argv[2],"%d %[^ /t]", &seed, &ch) != 1) ||
        (sscanf(argv[3],"%d %[^ /t]", &itt_max, &ch) != 1) ) {
      fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]); 
      return(1);
    }
  } else if(argc != 1 ) {
    fprintf(stderr," ERROR : useage: %s [ <n> <seed> <itt_max>]\n", argv[0]); 
    return(1);
  } 
  if( n<1 ) {
      fprintf(stderr," ERROR : n must be positive");
      return(1);
  }
  {
    double a[n][n]; // transformation matrix
    double b[n];    // transformation vector 
    double ts[n];   // solution vector
    double ts1[n];  // solution vector
    double *t = ts; // pointer to solution vector
    double *t1 = ts1;// pointer to next itteration of solution vector
    double *ttemp;  // used to swap t1 and t at each itteration
    //
    // Generate matrix a with | eigenvalues | < 1

    srand48((long int)seed);
    printf("\n  a=\n");
    for(i=0; i< n; i++) {
      for(j=0; j< n; j++) {
        a[i][j] = 1.999 * (drand48() - 0.5) / n;
        printf("%10.6f ", a[i][j]);
      }
      printf("\n");
    }
    printf("\n  b=\n");
    // Generate vector b 
    for(i=0; i< n; i++) {
      b[i] = 10.0 * drand48();
      printf("%10.6f ", b[i]);
    }
    printf("\n");
    // Initialize t
    for(i=0; i< n; i++) {
      t[i] = b[i];
    }

    ptrB = b;

    a1 = newArgs (n, 1, a, ptrB);
    printf("%d\n",a1->mtxsize);
    printf("%d\n",a1->threadID);

    for(int i=0; i<a1->mtxsize; i++){
       for(int j =0; j<a1->mtxsize; j++){
         printf("%10.6f ", a1.a[i][j]);
       }
       printf("\n");
    }

    a2 = newArgs (n, 2, a, ptrB);
    a3 = newArgs (n, 3, a, ptrB);
    a4 = newArgs (n, 4, a, ptrB);

  }

  return(0);
}

mtxargs *newArgs (int n, int id, double *(*a)[], double *b)
{
  mtxargs *args;

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      printf("%10.6f",a[i][j]);
    }
    printf("\n");
  }

  args = (mtxargs *)malloc (sizeof (mtxargs));
  if (args == NULL) return (NULL);
  args->mtxsize = n; args->threadID = id; args->a=a;args->b=b;
  return (args);
}

and am attempting to assign the arrays "a" and "b" to the struct mtxargs to be passed to each pthread. After hours of research, I can't seem to find how to do this efficiently at all, and keep coming across solutions that either fail when assigning to the struct due to pointer type incompatibility, or when I attempt to access the array from the struct. How would one go about assigning these two arrays to the struct? I'm relatively new to C, so more than just what works, please also tell me why. Note that I haven't invoked pthread_create yet, I want to get this working right first before launching off on that!

-SC

Sean Corbett
  • 39
  • 2
  • 5
  • A 2D array like `double a[n][n]` is not the same as an array of pointers, so it's not compatible with the pointer-to-array-of-pointer-to-double (`double *(*a)[]`) in your struct (`double a[n][n]` would decay to `double (*)[n]`, a pointer to arrays of `n` `double`s). Why not use `void *` in the struct, and then assign it to a local `double (*a)[n];` pointer to access the array (after the value of `n` is known)? – Dmitri Feb 19 '17 at 03:43
  • Related: https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – alk Feb 19 '17 at 11:17

0 Answers0