Array does not print in reverse in C with pointers

I am trying to make a program that takes 10 numbers as input and outputs them in reverse using pointers in C.

#include<stdio.h> #define N 10 int array[N]; //Global variable int main(void) { int j; int i; printf("Enter 10 numbers: "); for (i=0;i<N;i++) { scanf("%d",(array+(4*i))); //Works } for (j=N-1;j<0;j--) { printf("%d",array[j]); //Doesn't print, using *(array+j*4) doesn't //print also } printf("\n"); printf("%d\n",*(array)); //Works so scanf works printf("%d\n",*(array+4)); //Works so scanf works return 0; } 

I tried to make a separate function for two loops, but it still doesn't work. I want to know WHY this for loop does not print, but two printfs below print.

EDIT:

My new code

  #include<stdio.h> #define N 10 int array[N]; //Global variable int main(void) { int j; int i; printf("Enter 10 numbers: "); for (i=0;i<N;i++) { scanf("%d",(array+i)); //Works } for (j=N-1;j<0;j--) { //it is supposed to be j>=0 or j>0 WHY printf("%d",array[j]); //Doesn't print, using *(array+j) doesn't //print also } printf("\n"); printf("%d\n",*(array)); //Works so scanf works printf("%d\n",*(array+1)); //Works so scanf works return 0; } 

Thanks to all the posts, I better understand how indexing works in C, but printf doesn't work unless I change the for-loop condition (see above). WHY it does not work with the initial conditions, but with the latest conditions.

+4
source share
4 answers

Your array consists of 10 elements of type int (obviously). In array + i variable i not an offset in bytes. This is the index of the item. So when you read it like you do ( scanf("%d",(array+(4*i))) ), you basically read array [0], array [4], array [8], array [12] (here we no longer have the boundaries of the array, it causes memory corruption and may cause crashes), etc. Elements of the array [1], [2], [3], [5], etc. uninitialized. That is why your code is not working :)

UPDATE: And @ shilong-liu's note about array indices is also important. I did not notice this.

+2
source

Wow!

It:

 scanf("%d",(array+(4*i))); //Works 

very wrong and overwrites memory! Why are you multiplying the index? You do not need to do this, C can index by itself. It should be simple:

 scanf("%d", &array[i]); 

You need the address of the i : th member, so say don't beat around the bush with strange multiplications.

If you really want to “use pointers” as indicated in the comment, you can do this:

 scanf("%d", array + i); 

This works because array is a pointer to the first element of the array, and adding i to is completely legal using pointer arithmetic; C will calculate the correct pointer, knowing the size of each int in the array.

+8
source
 for (j=N-1;j<0;j--) { printf("%d",array[j]); //Doesn't print, using *(array+j*4) } 

the for loop is invalid. The correct thing is that

 for (j = N - 1; j > 0; j--) 
+1
source

I think, since the pointer used is of type int, you assume that you need to multiply i by 4, because depending on the compiler, int is 4 bytes. I think if you really only care about the exit, then you can do it the way you did with the reverse iteration.

What you need to do has already been mentioned by others, so I will give you my solution for actually replacing the pointer memory and you can choose from these solutions:

  #include<stdio.h> #define N 10 int array[N]; //Global variable int main(void) { int j; int i; printf("Enter 10 numbers: "); for (i=0; i<N; i++) { scanf("%d", (array + i)); } for (left = 0; left < N / 2; left++) { int right = N - left - 1; int temporary = array[left]; array[left] = array[right]; array[right] = temporary; } for (i=0; i<N; i++) { printf("%d", (array + i)); } return 0; } 

I programmed for algorithmic contests so you can trust me.

0
source

Source: https://habr.com/ru/post/1490661/


All Articles