Thank you for the updated version of the code.
Line 35:
|
void display(int random*, int size)
|
compiler error,
35 24 [Error] expected ',' or '...' before '*' token |
correct code should be:
|
void display(int * random, int size)
|
Ok now we have some code which we can run, let's see what happens when it is tested.
The array should contain values in the range 1 to 100, but that isn't what was displayed.
Here, I repeat what I said earlier,
loop at line 16 to 19 should not be changing the value of data by incrementing it. Use a separate pointer variable instead. |
Here's one way:
1 2 3 4 5 6
|
int * p = data;
for (int i = 0; i < arraySize; i++)
{
*p = rand() % 100 + 1;
++p;
}
|
Here's another way:
1 2 3 4
|
for (int * p = data; p < data + arraySize; p++)
{
*p = rand() % 100 + 1;
}
|
And a variation on that last one,
1 2 3
|
int * p = data;
while (p < data + arraySize)
*p++ = rand() % 100 + 1;
|
But what about the
function display()
, doesn't that also need to be changed to use a separate pointer? Well, no it doesn't.
In this case, the parameter
int * random
is passed by
value, which means it is already a separate copy of the pointer
data
in main.