如何以C来确定数组的大小?如何以C来确定数组的大小?也就是说,数组可以容纳的元素数?
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
执行摘要:
int a[17];size_t n = sizeof(a)/sizeof(a[0]);
全文答覆:
sizeof
int a[17];size_t n = sizeof(a);
int a[17];size_t n = sizeof(a) / sizeof(int);
a
sizeof(int)
sizeof(a[0])
int a[17];size_t n = sizeof(a) / sizeof(a[0]);
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))int a[17];size_t n = NELEMS(a);
aluckdog
TA贡献1847条经验 获得超7个赞
sizeof
sizeof
size_t size
#include <stdio.h>#include <stdlib.h>void printSizeOf(int intArray[]);void printLength(int intArray[]);int main(int argc, char* argv[]){ int array[] = { 0, 1, 2, 3, 4, 5, 6 }; printf("sizeof of array: %d\n", (int) sizeof(array)); printSizeOf(array); printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) )); printLength(array);}void printSizeOf(int intArray[]){ printf("sizeof of parameter: %d\n", (int) sizeof(intArray));}void printLength(int intArray[]){ printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));}
sizeof of array: 28sizeof of parameter: 8Length of array: 7Length of parameter: 2
sizeof of array: 28sizeof of parameter: 4Length of array: 7Length of parameter: 1
慕码人2483693
TA贡献1860条经验 获得超9个赞
sizeof
int a[10];int* p = a;assert(sizeof(a) / sizeof(a[0]) == 10);assert(sizeof(p) == sizeof(int*));assert(sizeof(*p) == sizeof(int));
- 3 回答
- 0 关注
- 1009 浏览
添加回答
举报
0/150
提交
取消