OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:28 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: C allocate and initialize in same statement
PostPosted: Wed Mar 09, 2022 5:38 pm 
Offline
Member
Member
User avatar

Joined: Mon Sep 21, 2020 9:51 am
Posts: 100
Location: Aboard the Enterprise
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

_________________
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!


Top
 Profile  
 
 Post subject: Re: C allocate and initialize in same statement
PostPosted: Wed Mar 09, 2022 6:13 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: C allocate and initialize in same statement
PostPosted: Thu Mar 10, 2022 11:02 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group