15,849,494 members
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 20:28pm

## Solution 1

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

v3

## Solution 2

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

v2

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

Top Experts
Last 24hrsThis month
 Richard MacCutchan 60 Graeme_Grant 40 OriginalGriff 30 Maxim Kartavenkov 20 merano99 20
 OriginalGriff 20 Richard MacCutchan 10 merano99 10 Maxim Kartavenkov 10 Dave Kreskowiak 10

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