在C中交换2个数组 [英] Swapping 2 arrays in C

查看:50
本文介绍了在C中交换2个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要交换一个函数中2个数组的值.问题是我可以更改主体中的任何内容,仅更改函数本身即可.它应该接收2个整数数组,并交换它们.问题是,我不知道数组的大小,出于我的理解,它们甚至可以是不同的大小.尝试以下代码:

I need to swap the values of 2 arrays in a function. The problem is I can change anything in the main, just the function itself. It should recive 2 integer arrays, and swap those. The problem is, that I don't know the size of the arrays, and for my understading they can even be in diffrent sizes. Trying this code:

    int main()
{
    int size = 4;  //Please notice that I'm using this only to print the array
    int a[] = {1,2,3,4};
    int b[] = {5,6,7,8};
    printArr(a,"a",size);
    printArr(b,"b",size);
    swapArray(a,b);
    printf("Swapped:\n");
    printArr(a,"a",size);
    printArr(b,"b",size);
}

和此功能:

 void swapArray(int **a,int **b)
{
    int *p = *a;
    *a = *b;
    *b = p;
}

而printArr仅打印数组:

while printArr simply prints the array:

void printArr(int arr[],char name[],int size)
{
    printf("%s:\t",name);
    for(int i=0;i<size;i++){
        printf("%d\t",arr[i]);
    }
    printf("\n");
}

我得到一个非常奇怪的结果:

I got a really weird result:

a:   1    2    3   4
b:   5    6    7   8
Swapped:
a:   5    6    3   4
b:   1    2    7   8

我想了解为什么会发生这种情况,而不仅是可​​行的解决方案.谢谢:)

I would like to understand why it happens, and not only a working solution. Thank you :)

推荐答案

在此次通话中

swapArray(a,b);

参数表达式的类型为 int * ,而函数参数的类型为 int ** .没有从类型 int * 到类型 int ** 的隐式转换.因此,编译器应发出诊断消息.

the argument expressions have the type int * while the function parameters have the type int **. There is no implicit conversion from the type int * to the type int **. So the compiler shall issue a diagnostic message.

在任何情况下,实现交换功能都是没有意义的.您的程序具有未定义的行为,至少是因为它尝试交换指针而不是数组本身.

In any case the swap function as it is implemented does not make sense. Your program has undefined behavior at least because it tries to swap pointers instead of the arrays themselves.

请考虑到数组不是指针,尽管在表达式中(极少数例外)确实会将数组隐式转换为指向其第一个元素的指针.

Take into account that arrays are not pointers though in expressions with rare exceptions they indeed are implicitly converted to pointers to their first elements.

要交换两个数组的元素,必须分别交换每对元素.并且您必须提供数组中元素的数量.否则,数组需要有一个哨兵值.

To swap elements of two arrays you have to swap each pair of elemenets separatly. And you have to supply the number of elements in the arrays. Otherwise the arrays need to have a sentinel value.

这里是一个演示程序,显示了如何定义函数交换.

Here is a demonstrative program that shows how the function swap can be defined.

#include <stdio.h>

void printArr( const int a[], size_t n, const char *s )
{
    printf( "%s:\t", s );

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

void swapArray( int *a, int *b, size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        int tmp = a[i];
        a[i] = b[i];
        b[i] = tmp;
    }
}

int main(void) 
{
    enum { N = 4 };
    int a[N] = { 1, 2, 3, 4 };
    int b[N] = { 5, 6, 7, 8 };

    printArr( a, N, "a" );
    printArr( b, N, "b" );
    putchar( '\n' );

    swapArray( a, b, N );


    printArr( a, N, "a" );
    printArr( b, N, "b" );
    putchar( '\n' );

    return 0;
}

其输出为

a:  1 2 3 4 
b:  5 6 7 8 

a:  5 6 7 8 
b:  1 2 3 4 

您可以使用指针交换原始数组的视觉表示.但是在这种情况下,数组本身将不会被交换.

You could swap visual representations of original arrays using pointers. But in this case the arrays themselves will not be swapped.

请考虑以下程序.

#include <stdio.h>

void printArr( const int a[], size_t n, const char *s )
{
    printf( "%s:\t", s );

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

void swapArray( int **a, int **b )
{
    int *tmp = *a;
    *a = *b;
    *b = tmp;
}

int main(void) 
{
    enum { N = 4 };
    int a[N] = { 1, 2, 3, 4 };
    int b[N] = { 5, 6, 7, 8 };

    printArr( a, N, "a" );
    printArr( b, N, "b" );
    putchar( '\n' );

    int *pa = a;
    int *pb = b;

    swapArray( &pa, &pb );

    printArr( pa, N, "pa" );
    printArr( pb, N, "pb" );
    putchar( '\n' );

    printArr( a, N, "a" );
    printArr( b, N, "b" );
    putchar( '\n' );

    return 0;
}

其输出为

a:  1 2 3 4 
b:  5 6 7 8 

pa: 5 6 7 8 
pb: 1 2 3 4 

a:  1 2 3 4 
b:  5 6 7 8 

如您所见,阵列未交换.但是,指向数组第一个元素的指针已交换.使用指针可以模拟数组的交换.

As you see the arrays were not swapped. However the pointers that point to first elements of the arrays were swapped. Using the pointers you can simulate swapping of arrays.

与C C ++相反,它具有模板函数 std :: swap ,用于数组,实际上可以像

Opposite to C C++ has a template function std::swap for arrays that can be called indeed simply like

std::swap( a, b );

这篇关于在C中交换2个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆