How to use function with pointer parameter to print an array??

Jul 20, 2013 at 6:44pm
The printArray function should take in the dynamically created array and the size of the array as parameters. It should print out the contents of the array.

#include <iostream>
#include <string>
using namespace std;
void printArray (int *, int);

int main()
{
int numStudents;
int *movies;

cout << "How many students were surveyed?" << endl;
cin >> numStudents;

while(numStudents <= -1)
{
cout << "Enter a positive number." << endl;
cin >> numStudents;
}
movies = new int[numStudents];

//cout << "Enter the number of movies watched by each student: ";

for(int i = 1; i <= numStudents; i++)
{
cout << "Enter number of movies watched by student " << i << " : ";
cin >> movies[i-1];

while( movies[i-1] < 0)
{
cout << "Enter a positive number: ";
cin >> movies[i-1];
}
}

printArray(movies, numStudents);

return 0;
}

void printArray (int *, int)
{


}



My problem is that I have no idea how to write the code to print the array using pointers. I've been stuck for awhile trying to figure it out.
Jul 20, 2013 at 10:45pm
It's probably better to explain with the actual solution.
1
2
3
4
5
void printArray(int* array, int sz)
{
    for (int i = 0; i != sz; ++i)
        std::cout << array[i] << std::endl;
}


When you pass an array to a function, you pass the address of the first element. That's why you need to pass the size, because the function only gets a pointer and cannot determine the size of the array from just having a pointer.

There's something we call pointer arithmetic where you can add/subtract values with a pointer. The really important thing to note is the type of the pointer. If you think about it, if a pointer is just a memory address, why does it have a type? It has a type so you can do pointer arithmetic with it.

Consider this:
1
2
3
size_t sz = 10;
char* p = new char[sz];
char* q = p + sz;

p is of type char, and a char has size 1 byte.
So the expression, p + sz has a value of 10 bytes past p.

Consider this:
1
2
3
size_t sz = 10;
int* p = new char[sz];
int* q = p + sz;

p is of type int, and let's say that an int has size 4 bytes.
So the expression, p + sz has a value of 40 bytes past p.

Now, the expression p + 1, is the same as p[1]; and the expression p + n, is the same as p[n]. It's pointer arithmetic that allows us to index into an array. The size of the type of the pointer is used as a multiplier on the index, and that's why pointers have a type.
Jul 21, 2013 at 12:43am
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
#include <iostream>

using namespace std;

void printArray ( const int *a, size_t n )
{
   for ( size_t i = 0; i < n; i++ ) cout << a[i] << ' ';
   cout << endl;
}

 int main()
 { 
    size_t numStudents;
    size_t *movies;

    do
    {
        cout << "How many students were surveyed: " << endl;
        cin >> numStudents;
    } while ( numStudents == 0 )
 
    movies = new size_t[numStudents];

    for( size_t i = 0; i < numStudents; i++ )
    {
        cout << "Enter number of movies watched by student " << i + 1 << " : ";
        cin >> movies[i];
    }

    printArray( movies, numStudents ); 

    return 0;
}


Topic archived. No new replies allowed.