I've been given this code for a struct:
struct Part /*A Part record*/
{
int ID;
float Price;
int Quantity;
};
typedef Part *Partptr;
// Part record pointer type; The type Partptr becomes synonymous with Part *
typedef Partptr* Index;
// The type Index becomes synonymous with Partptr *
I'm confused on what the typedefs below it are doing. Later in the program, I'm trying to define a dynamically allocated array of this struct using the provided variable Index DBindex
, but when I try doing so like this:
DBindex = new Index[];
I get an error:
a value of type "Index *" cannot be assigned to an entity of type "Index"
I thought Index was a pointer, based on the typedef. What am I missing here?
Partptr
is an alias for Part*
, and Index
is an alias for Partptr*
(aka Part**
).
Index DBindex = new Index[];
doesn't work, because new
returns a T*
pointer for whatever type of T
it creates, ie:
T* ptr = new T;
T* ptr = new T[size];
So, if DBindex
really needs to be an Index
(aka Part**
), then you need to new[]
a type that is equivalent to Part*
, such as Partptr
:
Index DBindex = new Partptr[size];
Which is equivalent to:
Part** DBindex = new Part*[size];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.