C 尝试交换阵列时运行失败的退出值5

C 尝试交换阵列时运行失败的退出值5,c,arrays,pointers,C,Arrays,Pointers,我试图交换阵列的每一半;我通过调用镜像函数5次并交换适当的值来切换数组的索引。我可以使用底部的注释代码来代替当前的函数代码,但是现在我收到并退出值5,我不知道为什么。我在代码中没有收到其他错误问题在于镜像功能。我没有使用for循环(也称为计数循环)将数组的每个元素移动到临时数组。我在函数中添加了两个循环,现在运行良好 /*Jeremy Johnson 11-18-48 * *The purpose of this program to to swap halves of an array.

我试图交换阵列的每一半;我通过调用镜像函数5次并交换适当的值来切换数组的索引。我可以使用底部的注释代码来代替当前的函数代码,但是现在我收到并退出值5,我不知道为什么。我在代码中没有收到其他错误

问题在于镜像功能。我没有使用for循环(也称为计数循环)将数组的每个元素移动到临时数组。我在函数中添加了两个循环,现在运行良好

/*Jeremy Johnson  11-18-48
 *
 *The purpose of this program to to swap halves of an array. So {1 2 3 4 5 6} 
 *becomes {4 5 6 1 2 3} using pointer notation.
 */

#include <stdio.h>
#include <string.h>

int array[] = {1, 2, 3, 4, 5, 6}; //initialize array
void mirror(int* array, int from_index, int to_index); //prototype statment

int main() {
    //define and assign pointer to array address   
    int *arrptr, i;
    arrptr = &array[0];

    //print original array
    printf("Original Array: \n");
    for (i = 0; i <= 5; i++) {
        printf("%d", array[i]);
    }
    printf("\n");

    //call function to swap values and mirror the array
    mirror(arrptr, 0, 2);
    mirror(arrptr, 3, 5);
    mirror(arrptr, 0, 5);
    mirror(arrptr, 1, 4);
    mirror(arrptr, 2, 3);

    //print final array
    printf("Mirror Array: \n");
    for (i = 0; i <= 5; i++) {
        printf("%d", array[i]);
    }
    printf("\n");

    return 0;
}

void mirror(int* array, int from_index, int to_index) {
    //create pointer for temporary memory storage  
    int *temp, c[1];
    temp = &c[0];

    //Place to_index in temporary memory  
    *(temp) = *(array + to_index-1);
    //Copy from_index to to_index  
    *(array + to_index-1) = *(array + from_index-1);
    //Copy temporary value back into from_index  
    *(array + from_index-1) = *(temp);

    return;
}

/* This code works for the function however I am not allowed to use array 
   notation. 

    c[1]=array[to_index];
    array[to_index]=array[from_index];
    array[from_index]=c[1];                  */
// perhaps swap the array by:

void mirror( int*, int* );

int main()
{
    ...
    int *pSecondHalf = array[(sizeof(array)/sizeof(int))>>1]; 
    // note: above line will not work for odd size array so:
    if( array[(sizeof(array)/sizeof(int)] & 0x01 ) 
    {
        pSecondHalf++; // middle term in odd size array will not move
    }

    int *pFirstHalf = array;

    const int *pLastHalf = array+((sizeof(array)/sizeof(int))>>1);

    // note: following 'for' loop will execute one extra time 
    // for odd size array, but nothing will be changed

    for( ; pFirstHalf < pLastHalf; pFirstHalf++, pSecondHalf++ )
    {
        mirror( pFirstHalf, pSecondHalf );
    }
    ...
    return(0);
}

// and in mirror()

void mirror( int *pVal1, int* pVal2)
{
    int temp = *pVal1;
    *pVal1 = *pVal2;
    *pVal2 = temp;
}

发送至c[1]的地址。我只是在那里临时存储一个从*数组+到_索引值。因此,temp指向c[1]的地址。最好包含与所用语言对应的标记。这看起来像C,所以我添加了这个标记。如果我错了,请纠正我。它确实是c,谢谢。这是家庭作业,对吗?只交换3次swaparray,0,3;斯瓦帕雷,1,4;斯瓦帕雷,2,5;交换为从镜像中移除-1。
/*Jeremy Johnson  11-18-48
 *
 *The purpose of this program to to swap halves of an array. So {1 2 3 4 5 6} 
 *becomes {4 5 6 1 2 3} using pointer notation.
 */

#include <stdio.h>
#include <string.h>

int array[] = {1, 2, 3, 4, 5, 6}; //initialize array
void mirror(int* array, int from_index, int to_index); //prototype statement

int main() {
    //define and assign pointer to array address   
    int *arrptr, i;
    arrptr = &array[0];

    //print original array
    printf("Original Array: \n");
    for (i = 0; i <= 5; i++) {
        printf("%d", array[i]);
    }
    printf("\n");

    //call function to swap values and mirror the array
    mirror(arrptr, 0, 2);
    mirror(arrptr, 3, 5);
    mirror(arrptr, 0, 5);

    //print final array
    printf("Mirror Array: \n");
    for (i = 0; i <= 5; i++) {
        printf("%d", array[i]);
    }
    printf("\n");

    return 0;
}

void mirror(int* array, int from_index, int to_index) {
    //create pointer for temporary memory storage  
    int *temp, c[6], j;
    temp = &c[0];

    //Place array elements in temporary memory 
    for (j = 0; j < 6; j++) {
        *(temp + j) = *(array + j);
    }

    //Place mirrored halves in array respectively
    for (j = 0; j <= (to_index - from_index); j++) {
        *(array + from_index + j) = *(temp + to_index - j);
    }

    return;
}