Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let us typedef single and two(multi) dimensional arrays respectively as below:
C++
typedef float VERTREX[3];
typedef VERTREX TRIANGLE[3];

then say I have initialized some VERTEX arrays,
VERTREX v1 = { 1, 2, 3 };
VERTREX v2 = { 2, 2, 3 };
VERTREX v3 = { 1, 2, 1 };

Assume mathematically a Triangle defined by combination of three vertices,therefore I defined a Triangle as following code snippet,
C++
TRIANGLE tr;

Problem arisen when I am going to assign each VERTEX(single dimension array) elements in to TRIANGLE(Array of arrays/2-Dimensional array) as below code,
C++
tr[0] = v1; // error C2106: '=' : left operand must be l-value(in Visual C++ compiler)

tr[1] = v2; //  error C2106:
tr[2] = v3; //  error C2106:

Also I cannot continue with creating array of Triangles too.
C++
TRIANGLE tr[4]; // creating array of Triangles

hence same behavior as expected.
If someone has an idea/solution how to assign Single Dimension array as an element of Two(Multi) Dimensional array please respond.Please do not provide solution with standard containers like std::vector or using raw pointers approach.
Please bound to array concept.
Thank you everyone for listening.


What I have tried:

Tried to create pointers to each Vertex arrays,but,it failed my optimization concept.
Posted
Updated 19-May-16 19:28pm

C++
typedef float VERTREX[3];
typedef VERTREX* TRIANGLE[3];

int main()
{
    VERTREX v1 = { 1, 2, 3 };
    VERTREX v2 = { 11, 21, 13 };
    VERTREX v3 = { 1, 12, 41 };


    TRIANGLE tr;
    tr[0] = &v1;
    tr[1] = &v2;
    tr[2] = &v3;
    tr[3] = &v3;  --(1)// Concept(Array size) violation - Expect Runtime error 

    std::cout << (*(&v3))[2] << std::endl;
    std::cout << (*tr[2])[2] << std::endl;

    system("Pause");
    return 0;
}


// Compile with Visual C++(v.120) with Warning Level 4 ,No warning if we eliminate the line (1), here output: 41 41
 
Share this answer
 
v3
C++
typedef float VERTREX[3];
typedef float* TRIANGLE[3];

int main() {

  VERTREX v1 = { 1, 2, 3 };
  VERTREX v2 = { 2, 2, 3 };
  VERTREX v3 = { 1, 2, 1 };

  TRIANGLE tr;

  tr[0] = v1;
  tr[1] = v2;
  tr[2] = v3;

  TRIANGLE triangle[4];
  return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900