It is currently Mon Oct 16, 2017 5:42 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: C programming question
PostPosted: Fri Jan 27, 2017 4:11 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3064
Location: Brazil
This is in the exgui.c from allegro's 4.4 source code:
Code:
   char buf1[256];
   char buf2[256] = "";

What's the meaning of buf2 statement???


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 4:19 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19083
Location: NE Indiana, USA (NTSC)
It allocates an array of 256 elements of type char, whose data is initialized to the empty string.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 4:25 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3064
Location: Brazil
tepples wrote:
It allocates an array of 256 elements of type char, whose data is initialized to the empty string.

News to me. :shock: I though I had to do ALWAYS one of these 2 ways...
Code:
1. memset(buf,0,256);
2. int i=0;
for(i=0;i<256;i++) buf[i]=0;


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 4:26 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5706
Location: Canada
It could also be obscure shorthand for initializing the array with all 0s. In C, if an array with a declared size is given an incomplete initializer, the remaining elements are filled with 0. Usually I see this done with the initializer {0} but "" should also work for the same purpose.

Though, if it's simply used as a string, then it's probably sufficient just that the first element is 0, i.e. an empty string.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 5:19 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3064
Location: Brazil
Well, usually if I create a buffer[256], it's filled up with junk, unless all the values are manually set to zeroes.
So, can I do something like char memory[0x2000]=0; ???


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 5:59 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19083
Location: NE Indiana, USA (NTSC)
Try this:
Code:
char memory[0x2000] = {0};


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 7:05 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5706
Location: Canada
If there's no initializer, it's not initialized at all. If there's 1 or more initializer, it's required to fill anything left over with 0s. What tepples just said will work.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 9:53 pm 
Offline

Joined: Mon Sep 27, 2004 2:57 pm
Posts: 1248
Huh, I've never heard of {0} before. Was that always there or is it a recent addition? (It just seems uncharacteristically convenient for C/C++, a type of not having to do something yourself that you'd expect from something higher level like C#)

Edit: Nevermind, I'm dumb.
char blah[] = {1, 2, 3, 4, 5};
I have heard of it, just not that particular use of it.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 10:02 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5706
Location: Canada
Drag wrote:
Was that always there or is it a recent addition?

It's been there since at least C89:

ansi.c.txt wrote:
3.5.7 Initialization

...

If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant.

...

If there are fewer initializers in a list than there are members of
an aggregate, the remainder of the aggregate shall be initialized
implicitly the same as objects that have static storage duration.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2017 10:33 pm 
Offline

Joined: Mon Sep 27, 2004 2:57 pm
Posts: 1248
I've been working with C# a lot lately, and using {array initialization} is something you do a lot, so therefore, I un-recognized it in C/C++ because there's lots of other convenient stuff in C# that you can't do in C/C++, I just forgot that this in particular actually was in C/C++.


Top
 Profile  
 
PostPosted: Mon Jan 30, 2017 5:50 am 
Offline
Formerly ~J-@D!~
User avatar

Joined: Sun Mar 12, 2006 12:36 am
Posts: 445
Location: Rive nord de Montréal
With C++ you are not even required to put something into the braces if you want to zero-initialize an array:
Code:
uint8_t buf[256] = {};


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: Pokun and 10 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® Forum Software © phpBB Group