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???
It allocates an array of 256 elements of type char, whose data is initialized to the empty string.
tepples wrote:
It allocates an array of 256 elements of type char, whose data is initialized to the empty string.
News to me.
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;
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.
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; ???
Try this:
Code:
char memory[0x2000] = {0};
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.
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.
Drag wrote:
Was that always there or is it a recent addition?
It's been there since at least C89:
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.
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++.
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] = {};