How to use a float in main that was declared in a function "body scope" on a function parameter before?

I can't for the life of me figure out how to use a float array declared inside function "body scope", then use the float array inside a function parameter which is inside main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <wherefoobarwouldbeprototyped.h>

//THIS FOO FUNCTION BELOW IS A USER DEFINED FUNCTION CREATED BY 
//ME WHICH WOULD BE SETTING UP STUFF FOR EXAMPLE A BUFFER TO THE 
//GPU ,ETC
 
void foo()
{
        float a[3];
}

int main(int argc, char *Argv[])
{
        foobar(a[0]);
         //the compiler will complain saying a is not defined the only way 
         // I know to fix this is by making float a[3]; 
         //global which is bad practice
        return 0;
}

Last edited on
A function isn't meant to "Store" data like you want it to do here (Anything declared in a function is of local scope to that function. This means when the function ends everything in that function will be gone). For that you should be using a class or struct. Instead a function is meant to process data usually for a specific task.

From your comments I gather that you want to use the function as a initialization function which will do some certain things that are needed to set something up.

If that is the case there is no reason why you would need to have the array defined in the function or even globally. Remember functions are for processing data not storing it.

So your best bet is to just move the array declaration into main and then pass that array into the function by pointer to do the initialization of it. Or better yet you could encapsulate this all into a class but since you didn't use any classes I am going to figure you would like to stay away from them.

Here is a quick example of what I am talking about.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void foo(float test[])
{
    // Do some setup work here on the array
    // anything done in this function will alter the array directly
}

int main()
{
    float a[10];

    foo(a];

    return 0;
}



Hope this helps a bit if you have any more question please feel free to ask I would be glad to help with what I can.
Last edited on
So if I was using a opengl function for example glGenBuffer(); which had a parameter which required a float I would use test[] as the function parameter inside the scope of foo like so?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <gl/gl.h>
void foo(float test[])
{
    // Do some setup work here on the array
    // anything done in this function will alter the array directly
   glGenBuffers(test[0],0,0,0);
}

int main()
{
    float a[10];

    foo(a[0]);

    return 0;
}
Last edited on
If it has a parameter that requires a float you would just pass in a float instead unless you wanted to function to decide which element to use.

For example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Passes in the first element in the array to the glGenBuffers function
void foo(float test)
{
    // Do stuff here
    glGenBuffers(test, 0, 0, 0);
}

int main()
{
    float testArray[2] = {1.0f, 2.0f};

    foo(testArray[0]);

    return 0;
}


I would suggest taking a step back and reviewing how arrays works and how functions and function parameters work in general. It seems like you kind of understand what they do but are missing a few key points.
Topic archived. No new replies allowed.