相关疑难解决方法(0)

按值将数组传递给函数

以下是C Programming Just the FAQs一书的摘录.这不是错误的,因为Arrays永远不能通过引用传递吗?

VIII.6:如何通过值将数组传递给函数?

答案:通过在被调用函数中声明带有方括号([])的数组名称,数组可以按值传递给函数.调用函数时,只需将数组的地址(即数组的名称)传递给被调用的函数即可.例如,以下程序将数组传递 x[]byval_func()value 命名 的函数:

int[]参数告诉编译器该byval_func() 函数将采用一个参数 - 一个整数数组.byval_func()调用该 函数时,将数组的地址传递给 byval_func():

byval_func(x);
Run Code Online (Sandbox Code Playgroud)

由于数组是按值传递的,因此会生成数组的精确副本并将其放在堆栈中.然后被调用的函数接收该数组的副本并可以打印它.因为传递给的数组 byval_func()是原始数组的副本,所以修改byval_func()函数中的数组对原始数组没有影响.

c arrays pass-by-reference pass-by-value

36
推荐指数
2
解决办法
8万
查看次数

为什么我的数组索引比指针快

为什么数组索引比指针快?指针不应该比数组索引快吗?

**我使用time.h clock_t来测试两个函数,每个函数循环200万次.

Pointer time : 0.018995

Index time : 0.017864

void myPointer(int a[], int size)
{
     int *p;
     for(p = a; p < &a[size]; p++)
     {
         *p = 0;
     }
}


void myIndex(int a[], int size)
{
     int i;
     for(i = 0; i < size; i++)
     {
         a[i] = 0;
     }
}
Run Code Online (Sandbox Code Playgroud)

c arrays indexing pointers

11
推荐指数
3
解决办法
6073
查看次数