0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    int x = 100, i;
    double D[x];

    for(i=0; i < 100; i++)
        scanf("%f", D++);

    return 0;
}

The code has two errors:

  1. "%f" rather then "%lf" - compilation error

  2. D++ - compilation error

But why is D++ an error? as D is a pointer to the first element of the array and ++ can be used on an array, like pointers?

3
  • 1
    You can't increment D because it is not a pointer. An array can decay to a pointer to its first element, but it's not a pointer in itself. What you probably want is something like &D[i] instead. Commented Apr 1, 2020 at 21:48
  • 1
    Also, when using scanf the format %f is for float. Commented Apr 1, 2020 at 21:49
  • 2
    Also think a little bit... If D really was a pointer, and you could do D++, then you would lose the original value of D. Once the loop was over, how would you access the data you just read, if you no longer have a pointer to it? Commented Apr 1, 2020 at 21:53

2 Answers 2

4

D is not a pointer to the first element of an array. D is an array, and an array "decays" into a pointer to its first element in most contexts.

The ++ operator cannot be used on an array because it modifies its operand, and an array is not modifiable (although its element are).

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

6 Comments

But we can use the arithmetics of array elements like points, for example *(D++)?
*(D++) is invalid for the same reason D++ is invalid, i.e. you're trying to increment an array. You're probably thinking of (*D)++ which increments the first element of the array.
When can we use an array with point arithmetics? We can use D+2 to get to the second element of the array, and &(D+2) to access the location of the second element of the array?
@gbox That's correct, you can do that because those expressions don't attempt to modify the array. ++ and -- aren't valid on arrays because they modify their operand.
&(D+2) is not a valid expression. & can only be applied to an lvalue, i.e. an addressable object, and D + 2 does not represent that but is just a pointer value.
|
0

you should know that D isn't pointer to first element of array but is a name of array and name of array has a special feature as it has the address of the first element in array but can't be incremented or decremented as it isn't real pointer but it is only the address of the first element in array SO keep in your mind ( douple D[x]; "name of array"= D=&D[0]) SO if you want to increment to scan array elements by user there is two method firstly by using array

 for(i=0; i < 100; i++)
        scanf("%lu", D[i]);

secondly by using pointer

douple *ptr=&D[0];
for(i=0; i < 100; i++)
        scanf("%lu", *(ptr+i);

As you want to fill array elements by values

 *(D+i)=*(ptr+i)=D[i] 

at the same i

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.