Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C programming language equivalent for structure initialization in C++

Tags:

c++

c

struct

Assuming list is some structure

list *temp;
temp = new list;

the above is in c++, does this have an equivalent in C?

like image 223
Michael Demse Avatar asked Jan 31 '26 03:01

Michael Demse


1 Answers

list *temp;
temp = malloc(sizeof(list));

[...]

free(temp);

C doesn't have the new keyword - you've got to allocate and free the memory you'd like to use manually. new also does some work behind the scenes, like calling constructors and returning the proper pointer type - malloc doesn't cover that, and has to be handled by the programmer.

like image 138
Nick Reed Avatar answered Feb 02 '26 17:02

Nick Reed



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!