使用带有指针的函数反转数组元素

时间:2019-02-17 19:36:06

标签: c arrays function pointers

我有一个数组[1,2,3,4,5],并且我试图使用带有指针的函数来反转其元素,但是我得到了错误的答案。 有人可以帮助我吗? 谢谢。

输出应为5、4、3、2、1。

我的代码:

#include <stdio.h>

int reverse(int *);                    // main function // 

int main(void)
{
    int arr[5] = { 1, 2, 3, 4, 5 };
    int i;                             // counter // 
    int w;                             // integer to store the values of the function // 

    for (i = 0; i < 5; i++) {
        w = reverse(arr);
        printf("the elements reversed will be = %d \n", w);
    }
}

int reverse(int *p2)
{
    int i, x;

    for (i = 4; i >= 0; i--) {
        x = p2[i];
        return x;
    }
}

0 个答案:

没有答案
相关问题