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?

4

2 Answers 2

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];
Sign up to request clarification or add additional context in comments.

Comments

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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.