Skip to content

Remove redundant variable definitions#3

Open
chocorho wants to merge 4 commits intosuwuako:mainfrom
chocorho:formatting/002-simplify-pointer-ops
Open

Remove redundant variable definitions#3
chocorho wants to merge 4 commits intosuwuako:mainfrom
chocorho:formatting/002-simplify-pointer-ops

Conversation

@chocorho
Copy link

@chocorho chocorho commented Jun 2, 2024

These arrays are already pointers to their first elements. So we can pass the arrays as pointers, and in fact, we should eliminate extra variables that point to the same memory loc!

Before merging, make sure it compiles and runs on your end. And maybe use a linter to make sure I'm not going crazy here...

I believe, and correct me if I am wrong, that these arrays are already pointers
to their first elements. So we can pass the arrays as pointers, and in fact, we
should eliminate extra variables that point to the same memory loc!
@suwuako
Copy link
Owner

suwuako commented Jun 3, 2024

doesn't appear to be compiling on my end - maybe its how my compiler on windows handles arrays?

.\cube_windows.cpp: In function 'int main()':
.\cube_windows.cpp:58:28: error: cannot convert 'double (*)[3]' to 'double*'
   58 |         draw_cube_vertices(cube_vertices, len_to_vertex, terminal_x, terminal_y, terminal_z);
      |                            ^~~~~~~~~~~~~
      |                            |
      |                            double (*)[3]
.\cube_windows.cpp:21:33: note:   initializing argument 1 of 'void draw_cube_vertices(double*, int, int, int, int)'
   21 | void draw_cube_vertices(double* p_cube_vertices, int len_to_vertex, int terminal_x, int terminal_y, int terminal_z);
      |                         ~~~~~~~~^~~~~~~~~~~~~~~
.\cube_windows.cpp:79:40: error: cannot convert 'double (*)[3][3]' to 'double*'
   79 |         group_triangles(cube_vertices, triangles);
      |                                        ^~~~~~~~~
      |                                        |
      |                                        double (*)[3][3]
.\cube_windows.cpp:26:58: note:   initializing argument 2 of 'void group_triangles(double (*)[3], double*)'
   26 | void group_triangles(double cube_vertices[8][3], double* p_triangles);
      |                                                  ~~~~~~~~^~~~~~~~~~~
.\cube_windows.cpp:82:50: error: 'p_cube_vertices' was not declared in this scope; did you mean 'cube_vertices'?
   82 |         loop(terminal_x, terminal_y, terminal_z, p_cube_vertices, cube_vertices);
      |                                                  ^~~~~~~~~~~~~~~
      |```
      

Vincent added 2 commits June 4, 2024 02:59
That variable has been deleted! Silly me.
Allow me to rever to the existing one instead.
I misremembered the rules for casting an array to a pointer type.
Upon refreshing myself of the rules for multi-dimensional arrays,
I believe I have a state that will compile on my friend's machine.

Am I right? We'll find out soon!
@suwuako
Copy link
Owner

suwuako commented Jun 4, 2024

new errors, still doesnt compile - getting closer to solving it!

.\cube_windows.cpp: In function 'int main()':
.\cube_windows.cpp:58:28: error: cannot convert 'double (*)[3]' to 'double**'
   58 |         draw_cube_vertices(cube_vertices, len_to_vertex, terminal_x, terminal_y, terminal_z);
      |                            ^~~~~~~~~~~~~
      |                            |
      |                            double (*)[3]
.\cube_windows.cpp:21:33: note:   initializing argument 1 of 'void draw_cube_vertices(double**, int, int, int, int)'
   21 | void draw_cube_vertices(double* p_cube_vertices[3], int len_to_vertex, int terminal_x, int terminal_y, int terminal_z);
      |                         ~~~~~~~~^~~~~~~~~~~~~~~~~~
.\cube_windows.cpp:79:25: error: cannot convert 'double (*)[3]' to 'double**'
   79 |         group_triangles(cube_vertices, triangles);
      |                         ^~~~~~~~~~~~~
      |                         |
      |                         double (*)[3]
.\cube_windows.cpp:26:30: note:   initializing argument 1 of 'void group_triangles(double**, double* (*)[3])'
   26 | void group_triangles(double *cube_vertices[3], double* p_triangles[3][3]);
      |                      ~~~~~~~~^~~~~~~~~~~~~~~~
.\cube_windows.cpp:82:50: error: cannot convert 'double (*)[3]' to 'double*'
   82 |         loop(terminal_x, terminal_y, terminal_z, cube_vertices, cube_vertices);
      |                                                  ^~~~~~~~~~~~~
      |                                                  |
      |                                                  double (*)[3]
.\cube_windows.cpp:25:67: note:   initializing argument 4 of 'void loop(int, int, int, double*, double (*)[3])'
   25 | void loop(int terminal_x, int terminal_y, int terminal_z, double* p_cube_vertices, double cube_vertices[8][3]);
      |                                                           ~~~~~~~~^~~~~~~~~~~~~~~
.\cube_windows.cpp: In function 'void draw_cube_vertices(double**, int, int, int, int)':
.\cube_windows.cpp:143:90: error: cannot convert 'double' to 'double*' in assignment
  143 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_x - pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double
.\cube_windows.cpp:145:90: error: cannot convert 'double' to 'double*' in assignment
  145 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_x + pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double
.\cube_windows.cpp:153:90: error: cannot convert 'double' to 'double*' in assignment
  153 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_y - pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double
.\cube_windows.cpp:155:90: error: cannot convert 'double' to 'double*' in assignment
  155 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_y + pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double
.\cube_windows.cpp:162:90: error: cannot convert 'double' to 'double*' in assignment
  162 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_z - pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double
.\cube_windows.cpp:164:90: error: cannot convert 'double' to 'double*' in assignment
  164 |                                         *(p_cube_vertices + (3 * vertex) + j) = centre_z + pythag_len;
      |                                                                                 ~~~~~~~~~^~~~~~~~~~~~
      |                                                                                          |
      |                                                                                          double```

This is an attempte to resolve compiler errors on suwa's end, by using
more consistent 2D array access.

However, I have yet to test this on a Windows machine, and I cannot
compile on Linux until we merge PR#4.
Do not be surprised when this fails to compile! I am simply trying to
"nudge" it in the right direction towards a compilable state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants