Beginners_-_Sizeof_array_size.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "raylib.h"
  2. void passarray(Vector2 pol[]);
  3. int main(void)
  4. {
  5. // Initialization
  6. //--------------------------------------------------------------------------------------
  7. const int screenWidth = 800;
  8. const int screenHeight = 450;
  9. InitWindow(screenWidth, screenHeight, "raylib example.");
  10. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  11. //--------------------------------------------------------------------------------------
  12. // Main game loop
  13. while (!WindowShouldClose()) // Detect window close button or ESC key
  14. {
  15. // Update
  16. //----------------------------------------------------------------------------------
  17. //----------------------------------------------------------------------------------
  18. // Draw
  19. //----------------------------------------------------------------------------------
  20. BeginDrawing();
  21. ClearBackground(RAYWHITE);
  22. //
  23. // Create a Vector2 array and create some points in it
  24. //
  25. Vector2 pol[3];
  26. pol[0] = (Vector2){100,100};
  27. pol[1] = (Vector2){20,300};
  28. pol[2] = (Vector2){180,300};
  29. //
  30. // Here we pass the Vector2 array into the function.
  31. passarray(pol);
  32. // Here we get the array size using sizeof.
  33. int size = 0;
  34. size = sizeof(pol)/sizeof(pol[0]);
  35. DrawText(FormatText("%i",size),10,10,20,BLACK);
  36. EndDrawing();
  37. //----------------------------------------------------------------------------------
  38. }
  39. // De-Initialization
  40. //--------------------------------------------------------------------------------------
  41. CloseWindow(); // Close window and OpenGL context
  42. //--------------------------------------------------------------------------------------
  43. return 0;
  44. }
  45. //
  46. // Fonction that takes a array of Vector2 for use in the function.
  47. void passarray(Vector2 pol[]){
  48. // Draw a triangle.
  49. DrawTriangle(pol[0],pol[1],pol[2],RED);
  50. }