Thread: Is it possible to pass a reference to a fixed size array of non primitive type?

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    Is it possible to pass a reference to a fixed size array of non primitive type?

    Hello. I want to pass to a certain function, a reference to a fixed size array of a user defined struct "Sphere":

    Code:
    void trace (Image& image, Sphere (&sphere_array) [3]) {
    However this results in:

    Code:
    main.cpp:39:29: error: invalid initialization of reference of type ‘Sphere (&)[3]’ from expression of type ‘Sphere [n_spheres]’
        trace (image,sphere_array);
                                 ^
    main.cpp:24:6: note: in passing argument 2 of ‘void trace(Image&, Sphere (&)[3])’
     void trace (Image&, Sphere (&) [3]);
    However,if I change
    Code:
    void trace (Image& image, Sphere (&inArray) [3])
    to:
    Code:
    void trace (Image& image, int (&in_array) [4])
    it works. Can't the compiler deduce the array size when using use defined types? Is that the cause? Is there a workaround to make it work? Anyway, I assume I will have to put the array inside a struct and pass it by reference instead, which probably I am going to do in the future anyway.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Can't the compiler deduce the array size when using use defined types?
    Of course it can. There must be some difference that you aren't disclosing. Are you perhaps using a variable-sized array for the Spheres?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    More in the spirit of C++ is to use std::array.

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    15
    Quote Originally Posted by john.c View Post
    Of course it can. There must be some difference that you aren't disclosing. Are you perhaps using a variable-sized array for the Spheres?
    No, just a fixed size one. Bellow the full code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct Vec3f {
        float x,y,z;
    };
    
    struct Image {
       int height,width,bpp;
    };
    
    struct Camera {
        Vec3f origin;
        Vec3f dir;
    };
    
    struct Sphere {
        Vec3f origin;
        Vec3f normal;
        float radius;
    };
    
    void trace (Image&, Sphere (&) [3]);
    
    main () {
       Image image;
       image.height=768, image.width=1024, image.bpp=4;
       int n_spheres=3;
       int test_array [n_spheres];
       Sphere sphere_array [n_spheres];
       sphere_array[0].origin.x=700,sphere_array[0].origin.y=700,sphere_array[0].origin.z=100;
       sphere_array [0].radius=100;
       sphere_array[1].origin.x=300,sphere_array[1].origin.y=300,sphere_array[1].origin.z=100;
       sphere_array[1].radius=100;
       sphere_array[2].origin.x=500,sphere_array[2].origin.y=500,sphere_array[2].origin.z=100;
       sphere_array[2].radius=100;
       trace (image,sphere_array);
    }
    
    void trace (Image& image, Sphere (&sphere_array) [3]) {
       unsigned char pixel_array [image.height*image.width*image.bpp];
       Camera camera;
        camera.origin.x=512, camera.origin.y=384, camera.origin.z=0;
        camera.dir.x=0, camera.dir.y=0, camera.dir.z=1;
        int recursion_level=0;
        for (int y=0; y<image.height; y++) {
            for (int x=0; y<image.width; y++) {
                camera.dir.x=x, camera.dir.y=y, camera.dir.z=1;
                for (int z=0; z<3; z++) {
    
                }
    
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    That isn't a fixed-sized array since n_spheres is a variable.
    pixel_array is also obviously variable-sized.
    You should be getting warnings about variable-sized arrays being forbidden in standard C++.

    I don't understand why you want to pass it as a reference.
    Code:
    #include <iostream>
    using namespace std;
     
    struct Vec3f {
        float x,y,z;
    };
     
    struct Image {
       int height, width, bpp;
    };
     
    struct Camera {
        Vec3f origin;
        Vec3f dir;
    };
     
    struct Sphere {
        Vec3f origin;
        Vec3f normal;
        float radius;
    };
     
    void trace(const Image& image, const Sphere sphere_array[]) {
        auto pixel_array = new unsigned char[image.height * image.width * image.bpp];
        Camera camera { {512, 384, 0}, {0, 0, 1} };
        for (int y = 0; y < image.height; y++) {
            for (int x = 0; y < image.width; y++) {
                camera.dir = {float(x), float(y), 1};
                for (int z = 0; z < 3; z++) {
                }
            }
        }
        delete[] pixel_array;
    }
     
    int main() {
       Image image = {768, 1024, 4};
       const int n_spheres = 3;
       Sphere sphere_array[n_spheres] = {
           { {700, 700, 100}, {0, 0, 0}, 100 },
           { {300, 300, 100}, {0, 0, 0}, 100 },
           { {500, 500, 100}, {0, 0, 0}, 100 }
       };
       trace (image, sphere_array);
    }
    Last edited by john.c; 08-17-2022 at 06:08 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Sep 2019
    Posts
    15
    Ah, I see know what you meant by "variable-sized array". A simple "const" solved it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-26-2020, 05:45 PM
  2. Pass array by reference help
    By Trey Brumley in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2014, 11:47 PM
  3. how can i pass 2d and 3d array by reference?
    By johnny8888 in forum C++ Programming
    Replies: 9
    Last Post: 10-23-2011, 10:04 AM
  4. how to pass array by reference
    By dezz101 in forum C Programming
    Replies: 13
    Last Post: 09-19-2010, 09:28 AM
  5. Use vector instead of fixed-size array
    By clegs in forum C++ Programming
    Replies: 19
    Last Post: 09-16-2007, 09:00 PM

Tags for this Thread