Simple array passing by reference

Status
Not open for further replies.

megadave

Ars Scholae Palatinae
1,042
Hi all:

Its 2:30 am and I can't figure this out. Maybe I'm just overlooking
the obvious here, so I hope u can help.

I pass a function an array by reference, and the function
assigns values to the array. For now the array is a static
predetermined size, and the assigned numbers aren't random yet.
(i will be implementing that next).

It appears only the first element in the array is assigned the value,
the rest are not set. What am I doing wrong?

and if I enable that printf line in the function, it gets to 22 then quits

Code:
#define MAX_CALLS 40

//in main

float acw[MAX_CALLS];  // the array to hold ACW times


void assignValues(float *acw)
{
  int i = 0;
  float value = 1;
  for(i = 0; i < MAX_CALLS; i++){
    acw[i] = value;
    //printf("Assigned  acw[%d] = %f\n", i, acw[i]);
  }

}

[This message was edited by megadave on May 10, 2002 at 02:36.]
 
I am not sure what the problem is. I tried the same thing that you are doing and it assigned all the values correctly.

However there is one possibility. What exactly do you mean when you say passing by reference?

are you calling the function like this:

setval(&acw);

or as

setval(acw);

the first invocation is actually incorrect.

Try the second one.

Hasan

edit:
Here is my code

Code:
 #include <stdio.h>

void setval(float *acw)
{
        int i = 0;
        for(i = 0; i < 40; i ++)
        {
                acw[i] = 4.0;
        }
}

int main(int argc, char** argv)
{
        int i = 0;
        float acw[40] = {0.0};
        setval(acw);
        for(i = 0; i < 40; i ++)
        {
                printf("acw[%d] = %f\n", i, acw[i]);
        }
        return 0;

}
 
Status
Not open for further replies.