Go Down

Topic: Tutorial on the basics of using the "buffer" instruction? (Read 2 times) previous topic - next topic

Coding Badly

But I am still unclear on the braces. How is this different than just using
Code: [Select]
int buffer_pos[NUM_AXES] = 0 ;


...does not compile.  For a simple data-type (like an "int") only one value is needed to initialize it because it can only store one value...

int iNeedOneValue = 13;

For an array data-type (like an array of "int") more than one value is needed to initialize it because it can store multiple values.  The braces are used to define the list of values used to initialize the array...

int iNeedFive[5] = { 97, 1678, 6, 169, 769 };

iNeedFive[0] is initialized to 97; iNeedFive[1] is initialized to 1678; etcetera.

Quote
I would like to be able to remember what the braces "mean" here.


You are telling the compiler, here's a list a values I want you to use to initialize my array.

Quote
Since I was unable to find " = { 0 }" anywhere else in lengthy Google searches of Arduino and C websites, I get the feeling this may be an "uncommon" use of this syntax


I suspect " = { 0 }" is rather uncommon for global / static data.  It is somewhat common for local / automatic arrays.

Nick Gammon

Quote
Since I was unable to find " = { 0 }" anywhere else in lengthy Google searches of Arduino and C websites, I get the feeling this may be an "uncommon" use of this syntax


It would be reasonably common. You need to search in the context of initializing arrays. Otherwise you find a lot of stuff about soccer scores.

http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

Whilst Coding Badly is correct that static variables are automatically set to zero, automatic variables aren't*. So this could be a reasonable way of initializing an array in a function:

Code: [Select]
void foo ()
{
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

// blah blah
}


*Edit: He didn't imply they were.

Zoandar

#17
Apr 01, 2011, 03:19 am Last Edit: Apr 01, 2011, 03:24 am by Zoandar Reason: 1

Quote
Since I was unable to find " = { 0 }" anywhere else in lengthy Google searches of Arduino and C websites, I get the feeling this may be an "uncommon" use of this syntax


It would be reasonably common. You need to search in the context of initializing arrays. Otherwise you find a lot of stuff about soccer scores.

http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

Whilst Coding Badly is correct that static variables are automatically set to zero, automatic variables aren't*. So this could be a reasonable way of initializing an array in a function:

Code: [Select]
void foo ()
{
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

// blah blah
}


*Edit: He didn't imply they were.



Again thank you all for the information. But I think the "magic bullet" may have just come from a single word in your post, when you just said " // start with all zero".

For whatever reasons this was not clear to me before, what he is doing with this rather cryptic instruction is setting ALL THREE of the array elements to ZERO in ONE STATEMENT. Correct?

If I am right, then, knowing that he had defined the constant NUM_AXES equal to 3, in this sketch interchanging these statements would both have the exact same effect:
Code: [Select]


int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

int buffer_pos[NUM_AXES] = { 0, 0, 0 };  // start with all zero



I eagerly await confirmation. :) (While I look at that link for array initialization - thank you!)

YES!!! Your link just verified the above. I wish the book's author had mentioned this, because it looks to be a VERY handy function. However, it also seems that he could just as simply used
Code: [Select]


int buffer_pos[NUM_AXES] ;  // start with all zero

OR

int buffer_pos[NUM_AXES] = {};  // start with all zero



Thanks!!!  :smiley-mr-green:
Zoandar
  ---v/\/\/\v---

Nick Gammon

Quote
... this rather cryptic instruction is setting ALL THREE of the array elements to ZERO in ONE STATEMENT. Correct?


Yes, setting the entire array to zero. More specifically, everything beyond the stated initializers. So to be pedantic, you requested the first position to be initialized to zero, and the compiler initialized the rest to zero for you, as you have stated that the array is to be initialized in the first place (by using "= ...").



However, it also seems that he could just as simply used
Code: [Select]


int buffer_pos[NUM_AXES] ;  // start with all zero

OR

int buffer_pos[NUM_AXES] = {};  // start with all zero




Your first example won't work with "auto" variables. That is, if they are defined inside a function. So it is dangerous to do that, because you might copy and paste them (from a statically defined place to inside a function). Also it relies upon you remembering that static variables are automatically initialized.

Your second example works but personally I think it is a bit cryptic.

I prefer:

Code: [Select]
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero


PaulS

Quote
Your second example works but personally I think it is a bit cryptic.

I prefer:

Code: [Select]
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

The important thing to keep in mind here is still that the number of explicitly valued positions in the array is limited to the number of explicit initializers provided. The rest of the positions in the array may or may not be valued with 0.

Prove this to yourself by changing where this statement is positioned (global, in a function, with and without the static keyword, etc.) and change the initial value to something other than 0, like 37. Then, use a for loop to print the index and value for each position in the array.

When the number of explicit initializers is less than the size of the array, the array values will NOT all be whatever (single) initializer is provided.

Go Up