function that creates and returns a 2d array wont work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

function that creates and returns a 2d array wont work

this is the error im getting(as a note playground\:25:20 continues for every definition of the array): ..\Playground\: In member function 'float* geometry::cube(float, float, float, float, float, float)': ..\Playground\:23:41: error: cannot convert 'float (*)[3]' to 'float' in initialization float _cube = new float[8][3]; ^ ..\Playground\:25:20: error: invalid types 'float[int]' for array subscript _cube[0] = {x, y, z}; and the code: float *cube(float x, float y, float z, float w, float h, float d){ float _cube = new float[8][3]; //i guess the error happens here the rest of the errors are because this didnt work in the first place _cube[0] = {x, y, z}; _cube[1] = {w, y, z}; _cube[2] = {x, h, z}; _cube[3] = {w, h, z}; _cube[4] = {x, y, d}; _cube[5] = {w, y, d}; _cube[6] = {x, h, d}; _cube[7] = {w, h, d}; return _cube; } as an example here is code that works: int *Point(int x, int y, int z){ int *_point = new int[3]; _point[0] = x; _point[1] = y; _point[2] = z; return _point; } in the main function i want to be able to do float *cube1 = cube(1, 2, 3, 4, 5, 6); then access cube1[0][0]; which would return 1 sorry if this is to much to read i just think all the info i presented is atleast somewhat necessary.

17th Dec 2018, 3:32 PM
frank
frank - avatar
4 Answers
+ 3
Its 2d so it needs tobe double pointer. float**cube=new float*[row]; Then on each row cube[0]=new float[3]; //row 0 You can use loop to make this easier
17th Dec 2018, 4:50 PM
Taste
Taste - avatar
+ 2
Its double pointer, simple explaination one * for one dimension 😄
17th Dec 2018, 4:58 PM
Taste
Taste - avatar
+ 1
Taste i see so more dimensions need more pointers
17th Dec 2018, 4:59 PM
frank
frank - avatar
0
Thank you Taste but why do i need 2 "*"?
17th Dec 2018, 4:56 PM
frank
frank - avatar