简体   繁体   English

静态分配的数组作为C中的函数参数

[英]statically allocated array as function argument in C

Can I do this in C: 我可以在C中执行此操作吗?

void myFunc(int *vp) {
    // do some stuff with vp
}

int main() {

    int v[5] = {1,2,3,4,5};
    myFunc(v);

    return 0;
}

I mean, what would be the correct? 我的意思是,正确的是什么? myFunc(&v); myFunc(&v); ?

Thanks!! 谢谢!!

Arrays decay to pointers when you pass them as arguments. 将数组作为参数传递时,数组会衰减到指针。 However, array decay is not the same as taking the address of an array. 但是,数组衰减与获取数组地址不同。

"Decay" is how some types are transformed when passed as function arguments. “衰减”是当作为函数参数传递时如何转换某些类型。 Even though v 's type is int [5] , it becomes int* when you pass it to a function. 即使v的类型为int [5] ,当您将其传递给函数时,它也会变为int* This is a behavior a lot of people don't like, but there's nothing to do about it. 这是很多人不喜欢的行为,但与此无关。

Note that, on the other hand, the type of &v is int (*)[5] , that is, a pointer to an array of 5 integers. 注意,另一方面, &v的类型是int (*)[5] ,即指向5个整数的数组的指针。 This type doesn't decay, that is, it doesn't transform automatically into another type if you pass it as a function parameter (and that's also why it wouldn't work if you used it in your example, since you need a pointer to integers, not a pointer to an array of integers). 此类型不会衰减,也就是说,如果将其作为函数参数传递,它不会自动转换为另一种类型(这也是为什么如果在示例中使用它,它也将不起作用的原因,因为您需要一个指针整数,而不是指向整数数组的指针)。

The "correct" thing to do (assuming decay is OK) is to do myFunc(v) , just as you're doing in your snippet. 要做“正确”的事情(假设衰减是可以的)是执行myFunc(v) ,就像您在代码段中所做的一样。 Keep in mind that you lose array bounds information when you do it. 请记住,这样做会丢失数组边界信息。

Yes ... Your code is correct. 是的...您的代码是正确的。

Here v==&v[0] array name is equal to address of first element of array 这里v==&v[0]数组名称等于数组第一个元素的地址

    myFunc(v); 

passing array name as argument that means you are passing address of first element in array. 将数组名称作为参数传递,这意味着您正在传递数组中第一个元素的地址。

    void myFunc(int *vp)  

Here you are using pointer. 在这里,您正在使用指针。 which store the address of first element of array which is passed so you can access the block which is covered with the array.by incrementing the pointer location. 其中存储了要传递的数组第一个元素的地址,因此您可以通过增加指针位置来访问数组覆盖的块。

And

    myFunc(&v);   

    &v==&&v[0];

&v is address of address of array first element. &v是数组第一个元素的地址的地址。

Now 现在

  void myFunc(int *vp)      

Here You got address of address of array first element, This is not pointing to array. 在这里,您获得了数组第一个元素的地址,这不是指向数组的地址。 Instead pointing some memory location.Now You can't access the array by incrementing the pointer. 而是指向一些内存位置。现在您无法通过增加指针来访问数组。

Your code is correct It will work.... But you should take extra care to check the boundary condition. 您的代码是正确的。它将起作用。...但是您应该格外小心以检查边界条件。 Please look through the code. 请仔细阅读代码。

void myFunc(int *vp) {
    vp[5] = 30;
}

int main() {

    int v[5] = {1,2,3,4,5};
    int a = 10;
    printf("Value of a before fun call %d\n", a);
    myFunc(v);
    printf("Value of a before fun call %d\n", a);
    return 0;
}

similarly 类似地

void myFunc(int *vp) {
    vp[5] = 30;
    myFunc2(vp);
}

void myFunc2(int *vp) {
    vp[6] = 30;
}

int main() {

    int v[5] = {1,2,3,4,5};
    int a = 10;
    printf("Value of a before fun call %d\n", a);
    myFunc(v);
    printf("Value of a before fun call %d\n", a);
    return 0;
}

This will result in segmentation fault due to stack curruption. 由于堆栈损坏,这将导致分段错误。 Since local variables are in stack. 由于局部变量在堆栈中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM