Alright, I'm guessing this is an easy question, so I'll take the knocks, but I'm not finding what I need on google or SO. I'd like to create an array in one place, and populate it inside a different 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;
}
}
}
That's an approximation to what my code is doing, but regardless, shouldn't be running into any overflow or out of bounds issues or anything. I generate an array:
double result[16];
for(int i = 0; i<16; i++)
{
result[i] = -1;
}
then I want to pass it to someFunction
someFunction(result);
When I set breakpoints and step through the code, upon entering someFunction
, results
is set to the same address as result
, and the value there is -1.000000 as expected. However, when I start iterating through the loop, results[n]
doesn't seem to resolve to *(results+n)
or *(results+n*sizeof(double))
, it just seems to resolve to *(results)
. What I end up with is that instead of populating my result array, I just get one value. What am I doing wrong?
EDIT
Oh fun, I have a typo: it wasn't void someFunction(double results[])
. It was:
void someFunction(double result[])...
So perhaps this is turning into a scoping question. If my double result[16]
array is defined in a main.cpp, and someFunction
is defined in a Utils.h file that's included by the main.cpp, does the result
variable in someFunction
then wreak havoc on the result
array in main?
EDIT 2:
@gf, in the process of trying to reproduce this problem with a fresh project, the original project "magically" started working.
I don't know how to explain it, as nothing changed, but I'm pretty sure of what I saw - my original description of the issue was pretty clear, so I don't think I was hallucinating. I appreciate the time and answers...sorry for wasting your time. I'll update again if it happens again, but for the meantime, I think I'm in the clear. Thanks again.