C Language Programming Modern Methods _ Array (eighth chapter)

Source: Internet
Author: User

The C language does not require checking the scope of the subscript. When the current label goes out of scope, the program may perform unpredictable behavior.

Look at this program:

#include <stdio.h>#defineN 10//intMain () {intA[n],i; printf ("Enter%d numbers:", N);  for(i=0; i<n; i++) scanf ("%d",&A[i]); printf ("In reverse order:");  for(i=n-1; i>=0; i--) {printf ("% d", A[i]); } printf ("\ n"); return 0;}

The program used a total of 4 times to Macro N: In the declaration of array A, in the printf function that shows the hint, there are two for loops

Can arbitrarily change the value of N, convenient, for the advantages of macro definition

Looking at this one:

#include <stdio.h>intMain () {intI,n; scanf ("%d",&N); intA[n]; printf ("Ebter%d numbers:", N);  for(i=0; i<n; i++) scanf ("%d",&A[i]); printf ("In reverse order:");  for(i=n-1; i>=0; i--) printf ("%d", A[i]); return 0;}

Do you think it's wrong?

Right and wrong is the difference between C89 and C99.

This method is a variable-length array of C99, which can be dynamically entered

C99, you can specify an initialization, such as

int a[15] = {0,0,29,0,0,0,0,0,0,7,0,0,0,0,48};

Because you want the element 2 to be 29, the element 9 is 7, the element 14 is 48, and all the others are 0. Errors may occur for large arrays

You can assign this value in C99

int a[15] = {[14] = 48,[9] = 7,[2] = 29};

In addition to making the assignment shorter and easier to read, the order of assignment is no longer a problem

You can do that.

int a[15] = {[+] =, [9] = 7,[+] =n};

The number in parentheses is the indicator

The designator must be an integer constant expression. If the array length to be initialized is n, the value of each indicator must be between 0 and n-1.

However, if the array length is omitted, the indicator can make any nonnegative integer, and for a later accesses than either case, the compiler infers the length of the group based on the largest indicator.

For example, the maximum value of the indicator is 23, so the length of the array is 24

int b[] = {[5] = 10,[23] = 13, [11] = 36, [15] = 29};

The default is 0 unspecified

C Language Programming Modern Methods _ Array (eighth chapter)

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.