1

I'm new to pointers...

While assigning values to an array using pointer we use:

int *arr;
arr = malloc(10*sizeof(int));
for(i=0;i<10;i++)
{
   scanf("%d",(arr + i));
}

But while assigning to a variable we use

int *arr = malloc(sizeof(int));
*arr = 10;

So why cant we use,

scanf("%d",*(arr + i));

Why is it showing error?

9
  • 3
    I don't see an array here. If a pointer was an array, it would not be called "pointer", but "array" (and vice versa). Commented Jul 18, 2016 at 19:27
  • 1
    In your last scanf example, you are dereferencing the pointer, whereas scanf expects an address (pointer). Commented Jul 18, 2016 at 19:30
  • An array can be a variable. OP is not using any arrays here. Commented Jul 18, 2016 at 19:33
  • You question is confused. See How to Ask and provide a minimal reproducible example. Any good C book will give the full picture you apparently don't have. Don't try learning C just by trial-error and from snippets on the internet. Commented Jul 18, 2016 at 19:34
  • @Olaf: "I don't see an array here." -- I do. I see an anonymous array object created by the malloc call. (Of course arr itself is a pointer object, not an array object.) Commented Jul 18, 2016 at 19:44

3 Answers 3

2

In

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

(arr + i) already is a pointer to the i-th element of the array arr. Had you scanfed into a "single" variable, you would have had to pass its address (=pointer to that variable).

scanf("%d", &some_int_variable); 

Why is it showing error?

Dereferencing:

scanf("%d",*(arr + i));

would be wrong, because it wouldn't pass the pointer to the i-th array member to read to, since scanf() requires a pointer to the argument specified by the format tag, not a value.

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

Comments

0

Because scanf requires a pointer. Not a value. *(arr+i) is the value so if you passed that to scanf the function could not change the value so that it was effective after the function call.

It requires a pointer so that it can change the value of the object pointed to and make the change effective after the call.

It is like all function calls in c. They pass by value. Inside the function the value may be changed but as soon as the function returns any such changes are lost.

However, if the value passed is the value of a pointer, the function may change the value of the object pointed to by the pointer. Such a change will still be effective when the function returns.

A simple example:

void f(int a)
{
    a = 10;  // This change is lost when the function returns
}

void g(int* a)
{
     *a = 10;  // This change will survive when the function returns
}

int main(void)
{
    int a = 0;
    f(a);
    printf("%d\n", a); // will print 0
    g(&a);
    printf("%d\n", a); // will print 10
    return 0;
}

Comments

0

scanf takes an address (pointer) of a value, the format specifier tells what kind of pointer.

scanf("%d", (arr + i) ) means you are giving scanf the address arr plus an offset of i,

when you write *arr = 10; you can also write it as *(arr + 0) = 10; so you are de-referencing the value and assigning it 10 in other words you cannot give this to scanf since it wants a pointer.

so

arr = malloc(10*sizeof(int));

      +---+---+---+---+---+---+---+---+---+---+
arr ->|0  |   |   |   |   |   |   |   |   | 9 |
      +---+---+---+---+---+---+---+---+---+---+

arr + i is an address in the range 0..9, de-referencing a value in the array you write *(a+i)

but

arr = malloc(sizeof(int));

      +---+
arr ->|   |
      +---+

writing *arr you are de-referencing the one value you allocated

*arr = 10; 

      +---+
arr ->| 10|
      +---+

however writing

scanf("%d", arr);

is fine since arr is a pointer that points to an integer and scanf takes a pointer.

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.