示例:
/**
* @brief how about array-unknown-count? show you here.
* @author wen`xuanpei
* @email 15873152445@163.com(query for any question here)
*/
#define _CRT_SECURE_NO_WARNINGS//support c-library in Microsoft-Visual-Studio
#include <stdio.h>
#include <stdlib.h>
#define print_integer_array(arr, n) ({ \
int i; \
putchar('{'); \
for(i = 0; i < n; i++) \
printf("%d,", arr[i]);/*read*/ \
printf("}\n"); \
})
#define fill_integer_array(arr, n, val) ({ \
int i; \
for(i = 0; i < n; i++) \
arr[i] = val;/*write*/\
})
int main(){
int const n = 4;
int *arr = (int*)calloc(n, sizeof(int));
if(arr){
arr[0] = 10, arr[2] = 30;
print_integer_array(arr, n);
arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4;
print_integer_array(arr, n);
fill_integer_array(arr, n, 100);
print_integer_array(arr, n);
free(arr), arr = NULL;
}
getchar();
return 0;
}
1)编译运行
2)要点分析
1)定义一个未知个数的数组,可以部分初始化,可以全部初始化
2)数组访问的范围是[0, n),n是数组元素的个数,注意不要越界
尾声:
其它不明白的地方不用过于纠结,那只是在浪费时间。学得多了,回过头来看自然融会贯通。