使用此代码:
#include <stdio.h>
void printSize(char *messages[]) {
printf("%d", sizeof(messages) / sizeof(char *));
}
int main(void) {
printf("Size when through passing direct to function: ");
printSize((char *[]){"Zero", "One", "Two", "Three"});
printf("\n");
printf("Size when calculating in main: %d\n", sizeof((char *[]){"Zero", "One", "Two", "Three"}) / sizeof(char *));
return 1;
}
我得到输出:
Size when through passing direct to function: 1
Size when calculating in main: 4
我知道没有办法在 char* 数组中获得正确数量的元素,但我很好奇为什么它们会给出不同的结果。