#Pointers

9 messages · Page 1 of 1 (latest)

olive raptor
#

#include<stdio.h>

int main() {
int r[99];
int i,n,k,j;
int temp;

printf("enter no of ele");
scanf("%d", &n);
printf("rotate how many times");
scanf("%d", &k);
for (i = 0; i < n; i++) {
    scanf("%d", &*(r+i));
}
for (i = 0; i < n; i++) {
    printf("%d", *(r+i));
}


for (i = 0; i < k; i++) {
    temp = r;
    for (j = n-1; j>=1; j--) {
        *(r + j) = *(r + (j - 1));

    }
    *(r+j) = temp;

}
for (j = 0; j < n; j++) {
    printf("%d", *(r + j));
}
return 0;

}

Programme to rotate the array k times using pointer. This is the question.
But im not getting the output.

willow totemBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dusk matrix
#

What are you trying to achieve with &(r+i)

#

If it's a pointer to element i you need to do r+i

#

And printing out r+i will give you said pointer, not the value, for that you need r[i]

#

So really your code should look like this ```#include<stdio.h>

int main() {
int r[99];
int i,n,k,j;
int temp;

printf("enter no of ele");
scanf("%d", &n);
printf("rotate how many times");
scanf("%d", &k);
for (i = 0; i < n; i++) {
    scanf("%d", r+i);
}
for (i = 0; i < n; i++) {
    printf("%d", r[i]);
}


for (i = 0; i < k; i++) {
    temp = r;
    for (j = n-1; j>=1; j--) {
        r[j] = r[j - 1]);

    }
    r[j] = temp;

}
for (j = 0; j < n; j++) {
    printf("%d", r[j]));
}
return 0;

}```

#

And don't think you're alone in thinking pointers are confusing, they were for me too at the beginning! 🙂

olive raptor
olive raptor
#

!solved