【发布时间】:2013-12-26 10:20:14
【问题描述】:
在将数组传递给函数后,我必须知道数组的大小。例如,
#include<stdio.h>
void func(char *ptr)
{
printf("%d\n",------); //Here i want the actual size of the array passed to ptr. What to fill in the blank to get total size of the arr[]
}
main()
{
char arr[10] = "Hello";
printf("%d\n",sizeof(arr)); // here it is giving 10 bytes that is equal to total size of the array
func(arr);
}
【问题讨论】:
-
您无法从指针中检索数组的大小。您需要将数组大小作为单独的参数传递,或者遍历数组元素,直到找到一个标记结束的标记值(就像
char*字符串一样) -
在 C 中,您应该将数组的大小传递给函数,因为在 C 中只传递了数组第一个元素的地址。
-
这就是为什么这么多函数都带有
size_t size参数的原因。 -
每个人都说一样的......我应该接受哪一个?
-
@Chinna;
Every one is saying same.... Which one should i accept?:在这种情况下应用FCFS(先到先服务)算法。 ;)