hey everyone!

having a small problem with my pointer.
I'm just trying to scan an array using a pointer but when i print it it shows me only zeros. Any help?

#include <stdio.h>


int main()
{
    int arr[10];
    int *ptr;
    int i;

    ptr = &arr[0];
    printf("Enter 10 integers:\n");
    for (i=0; i<5; i++){
        printf("arr[%d]: ", i);
        scanf("%d", ptr);
        ptr++;
    }

    for (i=4; i>=0; i--) {
        printf("arr[%d] = %d\n", i, *(ptr+i));
    }

}

I'm not entirely sure what your goal is from your words.
So this is a guess.

int main(){

    int arr[10];
    int *ptr;
    int i;
    ptr = arr;
    printf("Enter 10 integers:\n");
    for (i=0; i<5; i++){
        printf("arr[%d]: ", i);
        scanf("%d", ptr);
        ptr++;
    }
    for (i=4; i>=0; i--) {
        ptr--;
        printf("arr[%d] = %d\n", i, *ptr);
    }
    system("pause");
    return 0;
}

Hi yann.bohbot.9,

I'm just trying to scan an array using a pointer but when i print it it shows me only zeros. Any help?

first, your main function has a return type of int yet doesn't return any, as though you have declare it with return type void.

Secondly, though, your array arr[10] worked but it was not initialized. I would have written something like: int arr[10] = {0};

Then ptr = &arr[0] should be written as ptr = arr, because the name of the array is the address of the array first element or array first index, which is ofcourse a pointer.
so, if you run the below you see the obvious.

    int arr[10] = {0};
    int *ptr;
    int *ptr2;   // included for debug
    int i;

    ptr = &arr[0];
    ptr2 = arr;
    printf("Address of array name: < %p > \nAddress of array first element: < %p >\n",
             arr, &arr[0]
    );

    printf("Value of first pointer: < %p > \nValue of second pointer: < %p >\n",
             ptr, ptr2
    );

Finally, on your output. You are getting garbage values, don't forget you didn't initialize your array. And even if you do, you are not printing the value in the right address using your pointer.

The following could show you that:

    for (i=4; i>=0; i--) 
    printf("Address: < %p > Address of Array element: < %p > Value: %d\n",
            ptr+i, &arr[i], *(ptr+i));

     putchar('\n'); 

    for (i=4; i>=0; i--, --ptr) 
    printf("Value: %d Address: < %p > Address of Array element: < %p > \n", *ptr, ptr, &arr[i]);

I hope this helps.

Edited 2 Years Ago by 2teez

line 15 of the original post increments ptr within the loop, which is ok. But ptr is not reset back to the beginning of the array before the next loop starts on line 18. On line 19 ptr is beyond the end of the array so (ptr+i) is always invalid.

Fix the problem by copying line 9 to a new line just before line 18.

Then ptr = &arr[0] should be written as ptr = arr

It doesn't really matter one way or the other, both are correct and the compiler won't care which way you code it.

Edited 2 Years Ago by Ancient Dragon

Hey
Instead of creating an array and a pointer just create a pointer and allocate memory.
If you want to read a string from keyboard you can allocate some memory then read the input.

 char *str;
 str = (char *)malloc(sizeof(char)*20); // for 20 characters 

You can do the same for other data types.

Edited 2 Years Ago by Emre_1

Instead of creating an array and a pointer just create a pointer and allocate memory.

Why do you find that preferable in this case?

str = (char *)malloc(sizeof(char)*20); // for 20 characters 

There's no need to cast the return type of malloc. Void pointers are implicitly castable to any other point type.

This article has been dead for over six months. Start a new discussion instead.