C ++ - arrays as parameters, EDIT: now includes variable scope
Ok, I guess this is a simple question, so I'll take a knock, but I don't find what I need on google or SO. I would like to create an array in one place and fill it in another function.
I define a function:
void someFunction(double results[])
{
for (int i = 0; i<100; ++i)
{
for (int n = 0; n<16; ++n) //note this iteration limit
{
results[n] += i * n;
}
}
}
Which is an approximation of what my code is doing, but no matter should not be run into any overflow or out of bounds problems or whatever. I am creating an array:
double result[16];
for(int i = 0; i<16; i++)
{
result[i] = -1;
}
then i want to pass it someFunction
someFunction(result);
When I set breakpoints and execute the code, on login someFunction
, it is results
set to the same address result
as and the value is -1.000000 as expected. However, when I start to repeat the loop, results[n]
it doesn't seem to resolve, *(results+n)
or *(results+n*sizeof(double))
it just seems to be allowed *(results)
. What I get is that instead of filling the results array, I only get one value. What am I doing wrong?
EDIT
Oh fun, I got a typo: it wasn't void someFunction(double results[])
. It was:
void someFunction(double result[])...
Therefore, perhaps it turns into a substantive question. If my array double result[16]
is defined in main.cpp, as someFunction
defined in Utils.h file, which is included in main.cpp, if a variable changes result
in someFunction
harms result
array basically?
EDIT 2:
@gf, in the process of trying to reproduce this issue with a new project, the original project magically started working.
I don't know how to explain this as nothing has changed, but I'm pretty sure what I saw - my initial description of the problem was pretty clear, so I don't think I am hallucinating. I appreciate the time and the answers ... sorry to waste your time. I'll update again if this happens again, but for now I think I am explicit. Thanks again.
a source to share
Just specify part of the variable scope in the question - there is no variable scope issue here. the result / result in your definition of someFunction is a parameter -> it will take the value passed to. There is no communication between variables in the called function and it calls -> variables in the caller's function are unknown to the called function if they are not passed In addition, variable scope problems do not occur between subroutines in C ++ because there are no nested subroutines. The following code snippets would demonstrate the problems:
int i = 0;
{
int i = 0;
i = 5; //changes the second i, not the first.
//The first is aliased by the second i defined first.
}
i = 5; //now changes the first i; the inner block is gone and so is its local i
so if C ++ had nested subroutines this would cause the variable to change
void main()
{
double results[16];
double blah[16];
doSomething(blah);
void doSomething(double * results)
{
//blah doing something here uses our parameter results,
//which refers to blah, but not to the results in the higher scope.
//The results in the higher scope is hidden.
}
}
a source to share
It seems to me that your code should just work.
I just tried this in g ++ and it worked great. I guess your problem is elsewhere? have you tried the trim you sent?
#include <iostream>
void someFunction(double results[])
{
for (int i = 0; i<100; ++i)
{
for (int n = 0; n<16; ++n) //note this iteration limit
{
results[n] += i * n;
}
}
}
int main()
{
double result[16];
for(int i = 0; i<16; i++)
{
result[i] = -1;
}
someFunction(result);
for(int i = 0; i<16; i++)
std::cerr << result[i] << " ";
std::cerr << std::endl;
}
a source to share