// This is the header file
#ifndef HEADER_H
#define HEADER_H
#define DIM 100 // Here I define a costant I'm going to use in the declaration of the array (see next)
#include <iostream>
usingnamespace std;
// This is my record (it defines a "product"):
struct Product{
char code[3];
char description[50];
int qty;
};
// Now, this is the delicate part... I know for sure this typedefs allow me to correctly allocate the array need, but I want to better understand why:
typedef Product* PrPtr; // This should define the pointer-to-product (record)
typedef PrPtr Array[DIM]; // This should define a DIM-sized array of pointers-to-product
typedef Array *ArrPtr; // This should define a pointer to the previous array
/* ... prototypes ... */
#endif //End of the header file
// Functions file
#include "header.h"
usingnamespace std;
void Create_Array(ArrPtr array, int &n) // I give to the function the pointer to the array (I'm going to declare it globally) and the "size" of the array (see next)
{
array = new Array[DIM]; // This is the most delicate line
/* ... rest of the function ...*/
/* Then I'm going to use "n" NOT as size parameter for the array
* declaration, but as the limit in a for cycle to insert the records
* in the array! */
}
/* ... Main file and rest of the program ...*/
So, I got two questions:
1) Are those typedefs the correct way to define a dynamic array of dynamic elements? Is this the only way to get this result?
2) I don't think that using that costant in the header (DIM) is practically correct, because I'll allocate a big part of memory that I'm never going to use! Doesn't this make the dynamic allocation futile? Can I use the "n" parameter that I pass to the function as the size parameter for the array instead?
If I understood correctly this may solve my problem, but I cannot use external classes / templates, I'm binded to only use pointers and memory allocation operators (new/delete), anyway I thank you for getting interested (also, I'll save that link, I'll surely need it in future).
I tried to actuate my solution, but it doesn't work, the compiler says:
invalid use of array with unspecified bounds
And I'm sure it's because I modified the second typedef this way:
typedef PrPtr Array[];
(Also, I'm using the "n" parameter to build the array, as I supposed).
So, if this is not correct, how can I define it as an array pointer without defining immediately the boundaries of the array?
P.S.: I also receive another error from the compiler:
declaration of 'Prodotto* (* mag)[]' shadows a parameter
Is it possibile to exaplain me what does it mean without having to post the entire source code? ((1) May it be because of the bad pointer definition posted above?)
EDIT: (1) I think it's always because of that, because I get this error just on the array declaration in the function.
typedef PrPtr Array[DIM]; // This should define a DIM-sized array of pointers-to-product
Array a; //equivalent to Product *a[DIM]
Why do you need an array of pointers?
PrPtr array = new Product[n]; An array of products of size n. To use it in your function you need to pass the pointer by reference void Create_Array(PrPtr &array, int n);
declaration of 'Prodotto* (* mag)[]' shadows a parameter