Use of pointers

hi everybody!!

I'm trying to make use of pointers in this code but it didn't work! i followed a similar working codes.

I have an array of 50 cells and i passed it to a separate function to do the calculations. I passed as well 5 different floats to do the necessary calculations!

After running the programme it gives error message " reference to undefined variable or array "

Cheers!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <math.h>
 
/******************************************************************************/
void vo(float *a, float *b, float *c, float *d, float *e,float *array)
{
    float inc;
    inc=(*e);
      for (int i=0;i<50;i++)
      {
      *array = (*a) * ((*c)/((*c)+(*b))) * ( 1 - (exp((-(*e))*(((*b)+(*c))/((*d)*(*b)*(*c))))));
      (*e)=(*e)+inc;
      }
}
/******************************************************************************/

main ()
{
  float vin,r1,r2,c,t;
  float arr[50];
  
  vin=15;
  r1=15;
  r2=15;
  c=15;
  t=0.1;
  


        vo(&vin,&r1,&r2,&c,&t,arr);
        
          for (int i=0;i<50;i++)
    	   {
		 printf("%f\n",arr[i]);
    	   }
    
 		 return 0;
}

I have no problem running your program. The only thing I see is that you are writing 50 times to the first field of the array. All the other fields are not touched. And you are changing the value of t insight vo(). I am not sure if you are aware of this.

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
 
 
const int LENGTH = 50;

/******************************************************************************/
void vo(const float *a,const float *b,const float *c,const float *d, float *e,float *array)
{
    float inc=(*e);
    for (int i=0;i<LENGTH;i++)
    {
        *array = (*a) * ((*c)/((*c)+(*b))) * ( 1 - (exp((-(*e))*(((*b)+(*c))/((*d)*(*b)*(*c))))));
        (*e)=(*e)+inc;
        array++;		//moves the pointer to the next field of the array
    }
}
/******************************************************************************/

main ()
{
    float vin,r1,r2,c,t;
    float arr[LENGTH];

    vin=15;
    r1=15;
    r2=15;
    c=15;
    t=0.1;
   
    vo(&vin,&r1,&r2,&c,&t,arr);
    for (int i=0;i<LENGTH;i++)
    {
        printf("%f\n",arr[i]);
    }
    return 0;
}
Topic archived. No new replies allowed.