I'm doing programming exercises at the university and I'm with this:
Write a program that defines a type, PointerArray, a pointer to an ARRAY of a thousand positions whose stored data is a pointer to an int:
Declare and write a procedure, createArray, to which we pass a PointerArray variable and return the dynamic memory necessary to store 1000 integers.
Declare and write the code of a procedure that initializes, initializeArray, an ArrayPointer variable, which has already been passed to createArray (has the necessary dynamic memory) with consecutive values from 1 to 1000.
Declare and write the code of a procedure, writeArray, that given an ArrayPointer variable, and after calling createArray and initializeArray, write their values.
Write a main program that calls createArray, initializeArray, and writeArray in sequence.
I have this code:
#include <iostream>
#include <cabecera.h>
using namespace std;
int main()
{
PunteroArray array;
return 0;
}
void crearArray(PunteroArray& ar)
{
PunteroArray* ar = new PunteroArray;
}
void inicializarArray(PunteroArray& ar)
{
for(int i=0; i<1000;i++)
{
ar.a[i]=i++;
}
}
When doing build I get the following error:
declaration of 'ArrayPointer* ar' shadows a parameter ArrayPointer* ar = new ArrayPointer;
I understand that it means that I am reusing the ar variable, but then how do I allocate dynamic memory to my array in another way?
Thanks in advance.
Thanks to those who have been looking and thinking but I post my solution. I have been quite blind, it was a simple mistake.
Fixed code:
We simply had to allocate dynamic memory with another pointer of type PointerArray, and assign to it the address of our Pointer to array structure.
Here is the header with the struct declaration:
Thanks.
Trying to stick to the first definition:
my interpretation of that is:
where the type
PunteroArray
is a pointer to an array of 1000 objects of type ARRAY (each of which stores a pointer to an int).So, with that particular (whimsical?) definition, a possible solution is: