Passing an array of pointers to a function

I have this code, which passes an array of pointers to a function. My question is the comment at the end of the code.

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
int main()
{
   double a = 46;
   double b = 47;
   double c = 48;
   double d = 49;
   double e = 50;
   double f = 51;

   double *array[6] = {&a, &b, &c, &d, &e, &f};

stdDev(*array, 6);
}

double stdDev(double students[], int size)
{
   cout << &students[0] << endl; //shows a hex memory location, ok.
   cout << students[0] << endl; //shows 46, which is right
   cout << students[1] << endl; //shows -9.26...e+61

//All the numbers except 0 are missing from the array? 
//Other than [0], the rest appear to be accessing random memory locations, 
//like you've exceeded the bounds of the array.

}
Last edited on
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
#include <iostream>

double stdDev( const double values[], int size ) ;

int main()
{
    const int N = 6 ;
    double array[N] = { 46.1, 47.2, 48.3, 49.4, 50.5, 51.6 }; // array of values

    // pass the array to the function
    // (ie. pass the pointer to the first element and the number of elements)
    const double sigma = stdDev( array, N ) ;

    // use the value returned by the function
}

double stdDev( const double values[], int size )
{
    double result = 0 ;

    // to verify that we can access the elements of the array
    // that was passed, print out the contents of the array
    for( int i = 0 ; i < size ; ++i ) std::cout << values[i] << ' ' ;
    std::cout << '\n' ;

    // compute result

    return result ;
}
Thanks JLBorges, that's the way to do it without passing pointers. How an I make my example work with pointers?
> How an I make my example work with pointers?

Why do you want to work with pointers?
Did a career teacher insist that something silly must be done?

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
39
40
41
42
#include <iostream>

double stdDev( const double* const pointers[], int size ) ;

int main()
{
    const int N = 6 ;
    double v[N] = { 46.1, 47.2, 48.3, 49.4, 50.5, 51.6 }; // array of values
    
    double* array_of_pointers[N] = { v, v+1, v+2, v+3, v+4, v+5 } ; // array of pointers to values

    // pass the array of pointers to the function
    const double sigma = stdDev( array_of_pointers, N ) ;

    // use the value returned by the function
}

double stdDev( const double* const pointers[], int size )
{
    double result = 0 ;

    // to verify that we can access the elements of the array
    // that was passed, print out the contents of the array
    for( int i = 0 ; i < size ; ++i )
    {
        const double* ptr = pointers[i] ; // get the pointer

        if( ptr != nullptr ) // if the pointer is not null
        {
            double value = *ptr ; // get the value pointed to
            std::cout << value << ' ' ; // print it put
        }
        else // pointer is null
            std::cout << "nothing " ; // there is no value
    }

    std::cout << '\n' ;

    // compute result

    return result ;
}
Topic archived. No new replies allowed.