Error when deploying an array pointer in C ++

0

I'm doing college programming exercises and I'm with this:

  

Write a program in which a type is defined, PunteroArray, pointer to ARRAY of a thousand positions whose data   stored is a pointer to int:

     

 Declare and write a procedure, createArray, to which we pass   a variable of PunteroArray and return the necessary dynamic memory   to store 1000 integers.

     

 Declare and write the code of a procedure that initializes,   initializeArray, a PointerArray variable, which has already been passed to   createArray (has the necessary dynamic memory) with values   consecutive from 1 to 1000.

     

 Declare and write the code of a procedure, writeArray,   that given a variable of PunteroArray, and after calling   createArray and initializeArray, enter its 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 'PointerArray * ar' shadows a parameter        PointerArray * ar = new PointerArray;

I understand that means that I am reusing the variable ar, but then, how do I assign dynamic memory to my array in another way?

Thanks in advance.

    
asked by AguaSal 19.06.2017 в 22:05
source

2 answers

1

Trying to fit the first definition:

  

... that you define a type, PointerArray, pointer to ARRAY of a thousand positions   whose stored data is a pointer to int:

My interpretation of that is:

struct ARRAY {
    ARRAY() : n(new int) {}
    ~ARRAY() { delete n; }
    int* n;
};

// tipo PunteroArray, puntero a ARRAY de mil posiciones 
// cuyo dato almacenado sea un puntero a int:
typedef ARRAY(* PunteroArray)[1000]; 

where the type PunteroArray is a pointer to an array of 1000 objects of type ARRAY (which each of them stores a pointer to int).

So, with that particular definition (capricious?), a possible solution is:

#include <iostream>

struct ARRAY {
    ARRAY() : n(new int) {}
    ~ARRAY() { delete n; }
    int* n;
};

// tipo PunteroArray, puntero a ARRAY de mil posiciones 
// cuyo dato almacenado sea un puntero a int.
typedef ARRAY(* PunteroArray)[1000]; 


// recibe una variable de tipo PunteroArray sin inicialzar y
// devuelve la dirección de memoria libre reservada para 1000 ARRAYs
PunteroArray crearArray(PunteroArray& p)
{
    p = (PunteroArray)new ARRAY[1000];
    return p;
}

// asigna valores consecutivos de 1 a 1000
// a las direcciones apuntadas por los miembros n de cada p
void inicializarArray(PunteroArray p)
{
    for (int i = 0; i < 1000; ++i)
        *((ARRAY*)&p[0])[i].n = i+1;
}

// crea el puntero a array,
// lo inicializa y
// muestra en consola los elementos del array
void escribirArray(PunteroArray& p)
{
    for (int i = 0; i < 1000; ++i)
        std::cout << *((ARRAY*)&p[0])[i].n << '\n';
}

int main()
{
    PunteroArray p = nullptr;
    crearArray(p);
    inicializarArray(p);
    escribirArray(p);
    delete [] p;
}
    
answered by 20.06.2017 / 03:54
source
1

Thanks to those who have been watching and thinking but I publish my solution. I've been pretty blind, it was a simple mistake.

Corrected code:

#include <iostream>
#include <cabecera.h>

using namespace std;

int main()
{
    PunteroArray array;

    crearArray(array);

    inicializarArray(array);

    escribirArray(array);

    return 0;
}

void crearArray(PunteroArray& ar)
{
    PunteroArray* p = new PunteroArray; //aquí estaba el error
    p=&ar;//asignar la direccion de nuestra estructura a la memdin
}

void inicializarArray(PunteroArray& ar)
{
    for(int i=0; i<1000;i++)
    {
        ar.a[i]=i+1;
    }
}

void escribirArray(PunteroArray &ar)
{
    for(int i=0; i<1000; i++)
    {
        cout<<"dato:"<<ar.a[i]<<"\n";
    }
}

We just had to assign dynamic memory with another pointer of type PunteroArray, and assign to this the direction of our structure Pointer to array.

Here the header with the struct declaration:

#ifndef CABECERA_H
#define CABECERA_H

struct PunteroArray
{
    int a[1000];
    int* b=a; //no llego a entender la necesidad de este puntero si no he llegado a usarlo para resolver el problema
};

void crearArray(PunteroArray& ar);

void inicializarArray(PunteroArray& ar);

void escribirArray(PunteroArray& ar);


#endif // CABECERA_H

Thank you.

    
answered by 19.06.2017 в 22:26