0

I'm having trouble assigning a two dimensional float array to a pointer-to-pointer-to-float. I'm getting the warning

warning: initialization from incompatible pointer type

The assignment in question is at the bottom of the first code sample. I have changed my syntax around a bit based on stuff I've read on SO, but my problem persists. I'm trying to store data to represent the vertices and faces of a box, so I malloc'd a float** to hold all of the vertices, which worked, and then I malloc'd a float*** to hold a list of faces, where each face is defined by a list of vertices. Finally, I created an intermediary variable face0 which I want to store the list of vertices for a face and then assign that to face[0] like I did with anchors. This assignment does not work.

The assignment in question is at the bottom of the first code sample.

// I first create my list of vertices
// This part works
float **anchors;
int numAnchors = 8;

anchors = (float**)malloc(numAnchors*sizeof(float*));

// anchor0 etc are float arrays of length 3
anchors[0] = anchor0;
anchors[1] = anchor1;
anchors[2] = anchor2;
anchors[3] = anchor3;
anchors[4] = anchor4;
anchors[5] = anchor5;
anchors[6] = anchor6;
anchors[7] = anchor7;

// Now I want to create a list of box faces where
// each face is defined by four vertices.
float ***faces;
int numFaces = 6;

faces = (float***)malloc(numFaces*sizeof(float**));

float **face0 = {anchors[0], anchors[1], anchors[2], anchors[3]};
// warning: initialization from incompatible pointer type [enabled by default]

Based on what I have read here on SO, I have also tried replacing the last line with the following and got the following errors.

float face0[][] = {anchors[0], anchors[1], anchors[2], anchors[3]};
// error: array type has incomplete element type

and

float face0[][3] = {anchors[0], anchors[1], anchors[2], anchors[3]};
// error: incompatible types when initializing type ‘float’ using type ‘float *’

and

// foregoing the intermediary variable entirely
faces[0] = {anchors[0], anchors[1], anchors[2], anchors[3]};
// error: expected expression before ‘{’ token

I have also read the following which argued for an entirely different method of assignment:

conversion of 2D array to pointer-to-pointer

But I cannot even get the toy example to work as it is intended.

EDIT

This project can only use C syntax.

Luca
  • 515
  • 5
  • 17
  • I think you dont need to use `malloc()` second time. Since you are just 'pointing' the `faces` pointer to the memory location that was already issued by the first `malloc()` – zeekhuge Jun 24 '17 at 19:19
  • 2
    A pointer is not an array. `float **` is not the same as `float [][]`. And being a 3-star `***` programmer is not a compliment. It is a signal of a wrong interface. – too honest for this site Jun 24 '17 at 19:45
  • Why is my question being downvoted? I took painstaking efforts to make the problem and every attempted solution clear. Questions are only supposed to be downvoted if they are unclear or show lack of research effort. It seems that there is some other standard unknown to me by which my question is being evaluated. – Luca Jun 24 '17 at 19:52

1 Answers1

2

Just change it with this:

 float* face0[4] = {anchors[0], anchors[1], anchors[2], anchors[3]};
B. Parolin
  • 36
  • 1
  • 5