简体   繁体   English

我正在尝试反转 C 中的数组。 但是,代码将随机 1-2 抛出到一些非常大的数字

[英]I am trying to reverse an array in C. But, the code throws out random 1-2 to some very large numbers digit numbers

As the title states I am trying to make a program using C which asks user to input the array creates a new array, where the values in the array have been reversed.正如标题所述,我正在尝试使用 C 制作一个程序,该程序要求用户输入数组创建一个新数组,其中数组中的值已被反转。 For ex, Input: 10, 20, 30, 40 Output: 40, 30, 20, 10 I had written the following code for reversing the arrays,例如,输入:10、20、30、40 Output:40、30、20、10 我编写了以下代码来反转 arrays,

#include <stdio.h>
#define MAX_SIZE 100
int main()
{
    int sizeArray;
    int arr[MAX_SIZE];
    int * ptr = arr;
    printf("Enter Array size: ");
    scanf("%d", &sizeArray);
    printf("Enter Array elements:\n");
    for (int i = 0; i < sizeArray; i++)
    {
        scanf("%d", ptr + i);
    }
    printf("Copying to another array....\n");
    
    int newArr[MAX_SIZE];
    int * ptr2 = newArr;
    for (int i = 0; i < sizeArray; i++)
    {
        *(ptr2 + i) = *(ptr + sizeArray - i+1 );
    }
    printf("Printing new array:\n");
    for (int i = 0; i < sizeArray; i++)
    {
        printf("%d\n", *(ptr2 + i));
    }
    return 0;

For ex: When I input the values: 1, 2, 3, 4 The output is: 897546457, 1, 4, 3例如:当我输入值时:1、2、3、4 output 为:897546457、1、4、3

Please help me with what I am doing wrong here.请帮我解决我在这里做错的事情。

You complicate your life with these pointers, they are not useful here.这些指针会使您的生活复杂化,它们在这里没有用。 And use the [] operator for array access, it's easier.并且使用 [] 运算符进行数组访问,它更容易。

#include <stdio.h>
#define MAX_SIZE 100

int main(void)
{
    int sizeArray=6;
    int arr[MAX_SIZE] = {1, 2, 3, 4, 5, 6};

    printf("Copying to another array....\n");
    int newArr[MAX_SIZE];    
    for (int i = 0; i < sizeArray; i++)
    {
        newArr[i] = arr[sizeArray-i-1];
    }

    printf("Printing new array:\n");
    for (int i = 0; i < sizeArray; i++)
    {
        printf("%d\n", newArr[i]);
    }
    return 0;
}

In this statement在这份声明中

*(ptr2 + i) = *(ptr + sizeArray - i+1 );

the expression ptr + sizeArray - i+1 points outside the set of actual data then i is equal to 0 or equal to 1 because in this case you have表达式ptr + sizeArray - i+1指向实际数据集之外,然后i等于0或等于1 ,因为在这种情况下,您有

ptr + sizeArray - 0 +1

that is the same as那是一样的

ptr + sizeArray +1

and

ptr + sizeArray - 1 +1

that is the same as那是一样的

ptr + sizeArray

Also you did not reset the pointer ptr2 before this loop此外,您没有在此循环之前重置指针ptr2

printf("Printing new array:\n");
for (int i = 0; i < sizeArray; i++)
{
    printf("%d\n", *(ptr2 + i));
}

If you want to use pointers to copy in the reverse order one array in another then the code can look the following way如果您想使用指针以相反的顺序将一个数组复制到另一个数组中,那么代码可以如下所示

#include <stdio.h>

#define MAX_SIZE 100

int main( void )
{
    int arr[MAX_SIZE];
    int sizeArray;

    printf("Enter Array size: ");
    if ( scanf("%d", &sizeArray) != 1 )
    {
        sizeArray = 1; 
    }
    else if ( MAX_SIZE < sizeArray )
    {
        sizeArray = MAX_SIZE;
    }

    int *ptr = arr;

    printf("Enter Array elements:\n");
    for (; ptr < arr + sizeArray; ++ptr )
    {
        scanf("%d", ptr );
    }
    printf("Copying to another array....\n");
    
    int newArr[MAX_SIZE];
    int * ptr2 = newArr;
    
    while ( ptr != arr )
    {
        *ptr2++ = *--ptr;
    }

    printf("Printing new array:\n");
    for ( ptr2 = newArr; ptr2 < newArr + sizeArray; ++ptr2 )
    {
        printf("%d\n", *ptr2);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM