I am looking to allocate and initialize an array in the same statement. Not like calloc, where the members are initialized to 0, but to, say, 1, 2, and 3. (If it restricts anything, I want to do this to initialize a member of a struct.)
Thanks
C allocate and initialize in same statement
- PavelChekov
- Member
- Posts: 113
- Joined: Mon Sep 21, 2020 9:51 am
- Location: Aboard the Enterprise
C allocate and initialize in same statement
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe
Live Long And Prosper
Slava Ukraini!
Слава Україні!
The Final Frontier,
Space,
The Universe
Live Long And Prosper
Slava Ukraini!
Слава Україні!
-
- Member
- Posts: 5515
- Joined: Mon Mar 25, 2013 7:01 pm
Re: C allocate and initialize in same statement
You can't dynamically allocate and initialize an array in the same statement. Static and automatic allocation is fine, though. You need more braces if the array is a struct member.
Re: C allocate and initialize in same statement
As malloc can fail this would be very bad practice - even if it was allowed.
Why do you need to restrict it to a single statement?
Why do you need to restrict it to a single statement?