I think the answer is no - new in C++ is used for classes and structs to initialize a new instance, C has structs (only), and you declare them like any other variable type.
struct Person jon; // local variable - stack allocated
struct Person* ron = (struct Person *) malloc( sizeof( struct Person )); // pointer to struct - heap allocated
You can see the struct Name gets repeated a lot, it's normal to use a typedef in that situation to make that shorter, usually in the header:
typedef struct Person {
// structure members go here as normal
} Person;
There is also support for initializing the members during the definition (if you search for "designated initializer" that's probably the standard name for this):
struct Person {
char name[40];
unsigned int age;
};
int main() {
struct Person jon = { name: "John", age: 10};
struct Person joe = { .name="Joe", .age=20 };
struct Person jane = { "Jane", 30 };
}
new and delete work around a lot of the chores of using malloc and free, but are C++ only.
For further actions, you may consider blocking this person and/or reporting abuse
I think the answer is no -
new
in C++ is used for classes and structs to initialize a new instance, C has structs (only), and you declare them like any other variable type.You can see the
struct Name
gets repeated a lot, it's normal to use a typedef in that situation to make that shorter, usually in the header:There is also support for initializing the members during the definition (if you search for "designated initializer" that's probably the standard name for this):
new
anddelete
work around a lot of the chores of usingmalloc
andfree
, but are C++ only.