0
Pointers with array is not working with - backward operator, why?
#include <stdio.h> int main() { int a[5] = {22, 33, 44, 55, 66}; int *ptr = NULL; int i; ptr = a; for (i = 4; i >=0; i--) { printf("%d ", *(ptr - i)); } } showing address locations not array elements
4 Réponses
+ 1
As I mentioned before, use *(ptr + i) instead, since you want the locations that come after ptr, not before it.
https://code.sololearn.com/cuNfV70SCo5G/?ref=app
0
The issue is that ptr (and a) is a pointer to the beginning of the array, the first element. Decreasing this pointer by i means accessing elements that are out of the bounds of the array, i.e. random memory spaces with garbage values.
It should work with *(ptr + i) though.
0
what should I do to reverse print array elements
0
ty bro!