2023-06-04

Reversing array in C with pointers

Trying to reverse the array. And then print it

I am trying to reverse the array by indenting from last to first element of array and copy it to another array. arra will be the reversed array :

Here's my code :

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

int main()
{
    int num, *arr, i;
    scanf("%d", &num);
    arr = (int *)malloc(num * sizeof(int));
    for (i = 0; i < num; i++) {
        scanf("%d", arr + i);
    }
    
    int arra[] = {};
    /* Write the logic to reverse the array. */
    arra[0] = *(arr + num);
    for (int k = 1; k == num; k++) {
        int j = 1;
        arra[j] = *(arr + num -k);
        j++;
    }

    for (i = 0; i < num; i++)
        printf("%d ", *(arra + i));
    return 0;
}


No comments:

Post a Comment