首页 > 代码库 > C99新特性:Designated initializer

C99新特性:Designated initializer

代码:

 #include <stdio.h>
#include <stdlib.h>

#define LENGTH 10

// C99新特性:Designated initializer

void display(int arr[], size_t len);

int main(void) {

	int a1[LENGTH] = { 0, 0, 5, 0, 6, 7 };
	display(a1, LENGTH);

	int a2[LENGTH] = { [2] = 5, [4] = 6, 7 };
	display(a2, LENGTH);

	return EXIT_SUCCESS;
}

void display(int arr[], size_t len) {

	for (size_t i = 0; i < len; ++i) {
		printf("%2d", arr[i]);
	}

	printf("\n");
}

输出:

 0 0 5 0 6 0 0 0 0 0
 0 0 5 0 6 0 0 0 0 0


C99新特性:Designated initializer