当尝试使用函数交换 2 个指针的值来反转数组时,核心不断转储

发布于 2025-01-09 07:30:35 字数 789 浏览 1 评论 0原文

#include<stdio.h>

int main() {
    /* read integer array */
    int n, i;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    
    /* reverse the array */
    for (i = 0; i < n / 2; i++) {
        exchange(a[i], a[n - i - 1]);
    }

    /* print the array */
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }

    /* free the memory */
    free(a);

    return;
    
}

/* write a function that takes two pointers of two intergers and then exchanges the values
in those locations */
void exchange(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

这是代码,函数交换尝试交换这两个指针指向的整数的值。 不断地倾倒核心。

#include<stdio.h>

int main() {
    /* read integer array */
    int n, i;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    
    /* reverse the array */
    for (i = 0; i < n / 2; i++) {
        exchange(a[i], a[n - i - 1]);
    }

    /* print the array */
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }

    /* free the memory */
    free(a);

    return;
    
}

/* write a function that takes two pointers of two intergers and then exchanges the values
in those locations */
void exchange(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

This is the code, the function exchange tries to exchange the values of the integers which these two pointers are pointing to.
Keeps dumping the core.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人骨 2025-01-16 07:30:35

在此函数交换调用中,

exchange(a[i], a[n - i - 1]);

两个参数的类型均为 int,而函数需要的参数类型为 int *

您需要像这样调用该函数

exchange( a + i, a + n - i - 1);

In this call of the function exchange

exchange(a[i], a[n - i - 1]);

the both arguments have the type int while the function expects arguments of the type int *.

You need to call the function like

exchange( a + i, a + n - i - 1);
装纯掩盖桑 2025-01-16 07:30:35

您需要发送交换函数的地址值,因为它的参数被声明为指针。因此,您需要将此行更改

exchange(a[i], a[n - i - 1]);

为此行,因为正如我提到的,它需要地址。

exchange(&a[i], &a[n - i - 1]);

You need to send address values for exchange functions since arguments of it are declared as pointers. Therefore, you need to change this line

exchange(a[i], a[n - i - 1]);

to this line because as I mentioned it expects addresses.

exchange(&a[i], &a[n - i - 1]);
青衫负雪 2025-01-16 07:30:35

更好的方法是将整个数组和索引传递给交换。

像这样:在线尝试

#include<stdio.h>
#include <stdlib.h>

void exchange(int **arr, int n1, int n2);

int main() {
    /* read integer array */
    int n, i;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    
    /* reverse the array */
    for (i = 0; i < n / 2; i++) {
        exchange(&a, i, n - i - 1);
    }

    /* print the array */
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }

    /* free the memory */
    free(a);

    return 0;
    
}

/* write a function that takes two pointers of two intergers and then exchanges the values
in those locations */
void exchange(int **arr, int n1, int n2) {
    int temp = (*arr)[n1];
    (*arr)[n1] = (*arr)[n2];
    (*arr)[n2] = temp;
}

A nicer way would be passing the whole array and indexes to swap.

Like this: TRY IT ONLINE

#include<stdio.h>
#include <stdlib.h>

void exchange(int **arr, int n1, int n2);

int main() {
    /* read integer array */
    int n, i;
    scanf("%d", &n);
    int *a = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    
    /* reverse the array */
    for (i = 0; i < n / 2; i++) {
        exchange(&a, i, n - i - 1);
    }

    /* print the array */
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }

    /* free the memory */
    free(a);

    return 0;
    
}

/* write a function that takes two pointers of two intergers and then exchanges the values
in those locations */
void exchange(int **arr, int n1, int n2) {
    int temp = (*arr)[n1];
    (*arr)[n1] = (*arr)[n2];
    (*arr)[n2] = temp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文