C功能不起作用

时间:2017-03-01 20:22:38

标签: c

我是C编程的新手,我正在尝试一个函数来交换数组的变量,但我无法让它工作,我不知道为什么。你能帮我么?提前谢谢。

以下是代码:

#include <stdio.h>

void inplace_swap(int *x, int *y){
    if (x!=y){
        *y = *x ^ *y; 
        *x = *x ^ *y; 
        *y = *x ^ *y; 
    }
}

void reverse_array(int a[], int cnt){
    int first, last;
    for (first = 0, last = cnt-1; first <= last; first++, last--)
        inplace_swap(&a[first], &a[last]);
}

int main(){
    //int eight = 80, seven = 70;
    //inplace_swap(&eight, &seven);
    //printf("New eight value is %d\n", eight);
    //printf("New seven value is %d\n", seven); 

    int arr[5] = {1,2,3};
    reverse_array(&arr[3], 2);
    printf("%d\n", arr[1]);
    //int i;
    //for (i=0;i < 4;i++) {
    //  printf("%d\n", arr[i]);
    //}
}

4 个答案:

答案 0 :(得分:2)

reverse_array功能正常,但您无法在主要地方正确调用它:

你想要这个:

int main() {
  int arr[] = { 1,2,3 };
  reverse_array(arr, 3);

  int i;
  for (i=0;i < 3;i++) {
    printf("%d\n", arr[i]);
  }
}

您的代码中存在3个问题:

// here you declare an array of size 5 but you only initialize the first 3 elements
//This is not actually a problem in first place
int arr[5] = {1,2,3};     

// &arr[3] is the address of 4th element of the array, which has not
// been initialized, This actually swaps arr[3] with arr[4]
reverse_array(&arr[3], 2);

// here you print the second element which has not been modified
// at all by the reverse_array(&arr[3], 2);
printf("%d\n", arr[1]);

如前所述,XOR交换内容是老式的,仅适用于整数类型。

答案 1 :(得分:1)

从你的代码看来你想要交换数组中最后两个0,0的元素 您的5元素数组初始化为1,2,3,0,0 当你用'third'元素的地址调用swap函数时,它实际上是数组的第四个元素,你基本上交换了数组的第4和第5个元素,你实际上打印了数组的第2个元素

答案 2 :(得分:1)

此代码效果很好:

#include <stdio.h>

static void inplace_swap(int *x, int *y){
   if( x != y ) {
      *y = *x ^ *y;
      *x = *x ^ *y;
      *y = *x ^ *y;
   }
}

static void print_array( int arr[], size_t count ) {
   size_t i;
   for( i = 0; i < count; ++i ) {
      printf( "%s%d", i ? ", " : "", arr[i] );
   }
   printf( "\n" );
}

static void reverse_array( int a[], int cnt ) {
   int first, last;
   for( first = 0, last = cnt-1; first <= last; first++, last-- ) {
      inplace_swap(&a[first], &a[last]);
   }
}

int main() {
   int arr[] = { 1, 2, 3, 4, 5 };
   size_t count = sizeof(arr)/sizeof(arr[0]);
   print_array  ( arr, count );
   reverse_array( arr, count );
   print_array  ( arr, count );
}

执行追踪:

1, 2, 3, 4, 5
5, 4, 3, 2, 1

答案 3 :(得分:0)

需要了解所有评论和答案,以便了解您所犯的错误。话虽如此,我重新编写了一些代码以使其工作。请研究差异并提出问题......

#include <stdio.h>

/*** Prototypes - get in the habit of using them ***/
void inplace_swap(int *x, int *y);
void reverse_array(int *a, int cnt);
/***************************************************/

#define NUM_ELEMENTS(x)     (sizeof(x)/sizeof(x[0]))    

void inplace_swap(int *x, int *y)
{
    if (*x!=*y)
    {
        printf("Doing swap...\n");
        *y = *x ^ *y; 
        *x = *x ^ *y; 
        *y = *x ^ *y; 
    }
}

void reverse_array(int *a, int cnt)
{
    int first, last;

    for (first = 0, last = cnt-1; first <= last; first++, last--)
    {
        inplace_swap(&a[first], &a[last]);
    }
}

int main()
{
    int arr[] = {1,1,2};

    reverse_array(arr, NUM_ELEMENTS(arr));

    for ( int i = 0; i < NUM_ELEMENTS(arr); ++i )
    {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return(0);
}