4

I am trying to make a program that takes 10 numbers as input and outputs them in reverse order 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 have tried a making a seperate function for the two for loops but still it doesn't work. I want to know WHY this for-loop doesn't print but the two printfs below it print.

EDIT:

My new code is

    #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 have a better understanding of how indexing works in C now but the printf doesn't work still unless I change the for-loop conditions(see above). WHY doesn't it work with the initial conditions but with the latter conditions.

4
  • 1
    You concept: multiplication with 4 is wrong, you just need to add i to point to next location. (array+(4*i)) is wrong! just (array+ i) is sufficient. When you increments a pointer, it start pointing to next element of its type reagrdless of size. Commented Jul 10, 2013 at 8:17
  • A good read for you: 10.2 Pointers and Arrays; Pointer Arithmetic Commented Jul 10, 2013 at 8:20
  • 1
    the printf doesn't work still because for condition should be: j >= 0 because array index starts with 0, You are reading array from high index to low index (in reverse direction) Commented Jul 10, 2013 at 8:53
  • Thank you @GrijeshChauhan. I don't know how it skipped my mind, I will upvote your comment. Also, thank you for the read. Commented Jul 10, 2013 at 8:55

4 Answers 4

8

Whoa!

This:

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

is very wrong and is overwriting memory! Why are you multiplying the index? You don't need to do that, C can index by itself. It should just be:

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

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

If you really want to be "using pointers", as mentioned in a comment, you can do so:

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

This works since array is a pointer to the first element of the array, and adding i to is a fully valid use of pointer arithmetic; C will compute the proper pointer, knowing the size of each int in the array.

Sign up to request clarification or add additional context in comments.

5 Comments

If the assignment is about pointers, probably array+i is what is desired.
@jxh Good point (heh), thanks for pointing that out. I updated.
Thank you for your answer. I know that C can index by itself, but I still can't understand why my indexing method doesn't work. Isn't an every address in this array seperated by four bytes as it is an int array. I am just trying to figure out WHY it is not working. Also what do you mean by overwriting memory? Sorry if my questions are a bit noobish... :(
@user2466887 addition with pointers works differently than with numbers: if p is a pointer to int on a 32-bit machine, p + 1 is equal to (char*)p + 4.
@user2466887 In scanf a int you need address location where you wants to store, but for printf you just need value so in printf use *(array + i) with same format string.
2

Your array consists of 10 elements with type int (obviously). In expression array + i variable i is not an offset in bytes. It is an index of element. So when you read it like you do (scanf("%d",(array+(4*i)))) you basicly read array[0], array[4], array[8], array[12] (we're out of array bounds here already, it causes memory corruption and might cause crashes), etc. Elements array[1],[2],[3],[5], etc. are uninitialized. That's why your code doesn't work :)

UPDATE: And @shilong-liu's note about array indices is important, too. I didn't notice it.

1 Comment

That is a really good and simple explanation for array indexing. Thank you.
1
for (j=N-1;j<0;j--) {
    printf("%d",array[j]);  //Doesn't print, using *(array+j*4) 
}

the for loop is not right. The correct one is that

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

1 Comment

Actually it is supposed to be j>-1 in order to show all 10 values but WHY. Isn't j 9 at first then it starts to decrement until it reaches -1 then the loop stops as the condition is not satisfied ?
0

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

What you have to do has been already mentioned by the others so I will give you my solution for actually swapping the pointers memory wise and you could choose from the given 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 have been programming for algorithmic contests so you could trust me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.