首先,这里是一些代码:
int main()
{
int days[] = {1,2,3,4,5};
int *ptr = days;
printf("%u\n", sizeof(days));
printf("%u\n", sizeof(ptr));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法找出ptr指向的数组的大小(而不是仅仅给出它的大小,在32位系统上是4个字节)?
C中两个函数有什么区别?
void f1(double a[]) {
//...
}
void f2(double *a) {
//...
}
Run Code Online (Sandbox Code Playgroud)
如果我要在一个相当长的数组上调用这些函数,这两个函数的行为会不同,它们会占用更多的空间吗?
可能重复:
如何查找sizeof(指向数组的指针)
我知道sizeof运算符在编译时被评估并替换为常量.鉴于此,如何在程序的不同点传递不同数组的函数是否可以计算它的大小?我可以将它作为参数传递给函数,但如果我不是必须的话,我宁愿不必添加另一个参数.
这是一个例子来说明我的要求:
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF(a) ( sizeof a / sizeof a[0] )
void printarray( double x[], int );
int main()
{
double array1[ 100 ];
printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
printf( "The size of array1 = %ld.\n", sizeof array1 );
printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );
printarray( array1, SIZEOF( array1 ) );
return EXIT_SUCCESS;
}
void printarray( double p[], int s )
{
int i;
// …Run Code Online (Sandbox Code Playgroud) 我知道一个数组衰减到指针,如果一个声明
char things[8];
Run Code Online (Sandbox Code Playgroud)
然后在things其他地方使用,things是一个指向数组中第一个元素的指针.
另外,根据我的理解,如果有人宣称
char moreThings[8][8];
Run Code Online (Sandbox Code Playgroud)
那么moreThings它不是指向char的类型指针,而是类型为"指向char的指针数组",因为衰减只发生一次.
什么时候moreThings传递给一个函数(比如说原型void doThings(char thingsGoHere[8][8])实际上是什么进行了堆栈?
如果moreThings不是指针类型,那么这仍然是一个传递引用?我想我一直认为它moreThings仍然代表了多维数组的基地址.如果doThings接受输入thingsGoHere并将其传递给另一个函数会怎么样?
规则几乎是除非指定数组输入,const然后数组将始终可修改?
我知道类型检查的东西只发生在编译时,但我仍然对技术上作为一个引用传递的东西感到困惑(即只有当传递类型指针的参数时,或者指针数组是否为传递 - 参考也是?)
很抱歉这个问题在这个地方有点儿,但由于我很难理解这一点,很难说出一个精确的询问.