How do I initialize an array to a full 0?

Source: Internet
Author: User

In C, the statement that declares an array:

int arr[];

So the declared array is stored in random data that we don't know, which is garbage to the user. There are many situations where we want to initialize an array to all zeros in order to do something else.

The simplest way to do this is to use a looping bar array to set all elements to 0:

int arr[]; int 0 ;  for 0  ; i++)    0;  // This would make all ZERO

There are several other ways we can initialize an array to 0:

1, global variables and static variables are automatically set to 0 when initialized. If you declare a global variable, then he will become full 0 before running.

int arr[1024x768// This is globalint main (void) {     // Statements}

2, for the local array we also have shorthand initialization syntax. If an array is partially initialized, elements that are not initialized are automatically set to the corresponding type of 0. This is done automatically by the compiler. It can be written like this:

int Main (void) {    int arr[1024x768] = {0};  // This would make all ZERO     // Statements}

3 . You can also initialize the array with the Memset function at the beginning of the program. This command is especially useful if you have modified the array and want to reset it to full 0.

int arr[1024x768];arr[501024x768// This would reinitialize all to ZERO

How do I initialize an array to a full 0?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.