215k views
4 votes
Can Structs be instantiated without the 'new' keyword?

User Arif Arifi
by
7.6k points

1 Answer

3 votes

Final answer:

Yes, structs in the C programming language can be instantiated without the 'new' keyword. They are value types that can be created directly on the stack or as members of other structs or classes.

Step-by-step explanation:

Yes, structs in the C programming language can be instantiated without the 'new' keyword. Unlike classes, which require the 'new' keyword to create objects dynamically on the heap, structs are value types and can be instantiated directly on the stack or as members of other structs or classes.

Here is an example:

struct Point { int x; int y; };

int main() { struct Point p; p.x = 5; p.y = 10; return 0; }

In this example, we define and instantiate a struct called 'Point' without using the 'new' keyword. We declare a variable 'p' of type 'struct Point' and access its members 'x' and 'y' to assign values. This is a valid way to create and use a struct object in the C programming language.

User Tausif
by
7.0k points