Understanding Claims in C

I look at some of the C codes. Some of them are a little hard to understand. For example, what does the following assignment do:

MY_TYPE my_var[3]={0}; 

MY_TYPE is some fixed-point arithmetic type. I have not yet seen variables with [] brackets and assignments with {} around values.

It was too easy, I think. So what is the advantage of defining

 MY_TYPE my_var[3]={0}; 

above this:

 my_type my_var[3]; 
+4
source share
6 answers

It creates an array my_var type MY_TYPE , which is 3 in size and initialized for all 0s (I suspect MY_TYPE is a kind of integer type). Please note that for the rest of the information, only one initialization is required.

Also note that if you declare an array globally, and not inside a block, it will be initialized automatically, and this MY_TYPE my_var[3]; will be sufficient.

+4
source

This is an array of 3 elements, all of which are initialized to 0.

+4
source

MY_TYPE my_var[3]={0}; initializes the my_var array as follows:

my_var [0] = 0; my_var [1] = 0; my_var [2] = 0;

+2
source

it is a 1 dimensional array of 3 elements, initialized to 0. Technically, when you initialize one element of the array, all other elements are automatically initialized to 0.

So, 3 elements with 3 indices:

 my_var[0]=0; my_var[1]=0; my_var[2]=0; 

My_TYPE can be int , char or any other data type. Hope this helps.

Read about arrays here: http://www.cplusplus.com/doc/tutorial/arrays/

+2
source

my_var[3] is a variable of type MY_TYPE , which can store three values โ€‹โ€‹of the same type (called Array). The braces {} are used here as an initializer . my_var[3] = {0} initializes its first element to 0 . The most recent of them is initialized to zero.

  MY_TYPE my_var[3]; 

reserves three memory spaces for MY_TYPE data. While;

  MY_TYPE my_var[3] = {0}; 

initializes all of these three spaces to 0 .

+1
source

Advantage of using

 my_type my_var[3]={0}; 

over

 my_type my_var[3]; 

The first statement initializes the array. Without an initializer, your array will contain garbage values โ€‹โ€‹(regardless of what was previously in these memory cells).

+1
source

Source: https://habr.com/ru/post/1488091/


All Articles