1

I am having a problem with assigning a pointer to a 2d array located in a struct in C. The code runs but I am getting a compilation error and can not understand how to get rid of it.

First, the struct

typedef struct{
  double (*cases)[9];
} myStruct;

I then declare a struct of type myStryct and an array in my main program and try to set the pointer in my struct to point at this array:

 myStruct a;
 double myArray[5][9] = {
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
 }
 a.cases = &myArray;

The program run fines and I can access elements via the pointer but I get a compilation error: Warning: assignment from compatible pointer type. What is the problem here?

Euklides
  • 564
  • 1
  • 10
  • 35

2 Answers2

1
myStruct a;
double myArray[5][9] = {
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
   {0, 1, 2, 3, 4, 5, 6, 7, 8},
};
a.cases = &myArray[0];
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

problem:

void allocate(int ** universe,int n) // to assign pointer "a"  a nXn matrix //
{ 
   universe=(int **) calloc(n,sizeof(int *));
   cout<<"work3";
   int l;
   for(l=0;l<n;l++)
   universe[l]=(int *)calloc(n,sizeof(int));
}
MahaSwetha
  • 1,058
  • 1
  • 12
  • 21
Parikshit
  • 13
  • 3