Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we are again creating struct variable from struct object?

I am beginner in c++. Here is my doubt why in this program they are again creating struct variable from the previously created struct object ? Here is the example:

typedef struct prog1
{

int a,b;

}*obj1;

int main()
{

obj1 moc=new prog1(); // why again creating object for *obj1 object?
moc->a=10;  // why dont we use obj1 -> a=10;

}

thanks

like image 857
RaHuL Avatar asked Aug 01 '26 20:08

RaHuL


1 Answers

obj1 is not an object but a type definition, because it is part of the typedef definition. Namely, it is a type of prog1* (a pointer to prog1). The obj1 moc declares a variable of this type, i.e. moc is a pointer to prog1.

like image 96
Andrey Semashev Avatar answered Aug 04 '26 11:08

Andrey Semashev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!