All the elements of the C language array initialized to the same value

  This problem has plagued me for a long time, I always used to control the set -1 for; because I will not use memset (zu ﹏⊂) I am konjac. Today finally learned a little fur, and quickly recorded it

method one:

  Simple and crude, efficient and effective. for circulating a little bit is set to 1, this approach has been effective, no restrictions, unless you do not know the size.

Method Two:

  Establish the time when the array is initialized, if you can only create an array is a static array is initialized to 0;

   1 int arr[10] = {0};

  If the creation of a dynamic array can be dynamically allocated memory with stdlib in the calloc, open space after the default set to 0,

  But second method can only be set to zero.

Method three:

    Lane string with memset function to handle, but because it is so with four bytes of each byte int assignment, only Past, but because the assignment of bytes 0 and -1 can be used no effect set to 0 or 1;

  Why it can be 0 and -1, -1 as BCD code is then compared 32 1,0 32 0, although it is truncated to the byte memory write but int read are the same value so that it does not matter a.

  1 even think about its BCD code is a 31 1 0 ...... This error can fly to heaven.

   1 memset(arr, (-1), sizeof(arr)); 

 

  Probably Jiang Zi, (because these will only wrote here)

 

Easy to learn, gentlemen encourage each other!

Guess you like

Origin www.cnblogs.com/daker-code/p/12081921.html