C programming question

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

C programming question

Post by Zepper »

This is in the exgui.c from allegro's 4.4 source code:

Code: Select all

   char buf1[256];
   char buf2[256] = "";
What's the meaning of buf2 statement???
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C programming question

Post by tepples »

It allocates an array of 256 elements of type char, whose data is initialized to the empty string.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: C programming question

Post by Zepper »

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: Select all

1. memset(buf,0,256);
2. int i=0;
for(i=0;i<256;i++) buf[i]=0;
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C programming question

Post by rainwarrior »

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.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: C programming question

Post by Zepper »

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; ???
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C programming question

Post by tepples »

Try this:

Code: Select all

char memory[0x2000] = {0};
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C programming question

Post by rainwarrior »

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.
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: C programming question

Post by Drag »

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.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C programming question

Post by rainwarrior »

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.
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: C programming question

Post by Drag »

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++.
User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 569
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: C programming question

Post by Jarhmander »

With C++ you are not even required to put something into the braces if you want to zero-initialize an array:

Code: Select all

uint8_t buf[256] = {};
((λ (x) (x x)) (λ (x) (x x)))
Post Reply