-3
#include <stdio.h>

int main(void) {
    int values[10];
    int a = 10;

    values = &a;
    printf ("the value is = %i.\n\n", *values);

    return 0;
}

This code is written just for experimenting on pointers, I have just started learning it. My question is that if the name of the array is a pointer then why cant we copy some other variable's address into it.

The error that it gave was "assignment to expression with array type"

please explain it in simple way.

chqrlie
  • 131,814
  • 10
  • 121
  • 189

2 Answers2

1

Array designators are non-modifiable lvalues. You may not use an array designator in the left side of the assignment expression.

Thus the compiler issues an error for this statement

 int values[10];

 int a = 10;

 values = &a;
 ^^^^^^^^^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Arrays cannot be assigned to. You can store values into array elements and you can store array addresses into pointers, but arrays themselves cannot appear on the left side of an assignment operator.

You can change values to a pointer:

#include <stdio.h>

int main(void) {
    int *values;
    int a = 10;

    values = &a;
    printf ("the value is = %i.\n\n", *values);

    return 0;
}

or you can store a into values[0]:

#include <stdio.h>

int main(void) {
    int values[10];
    int a = 10;

    values[0] = a;
    printf ("the value is = %i.\n\n", *values);

    return 0;
}

think of arrays as parking lots:

  • You can store a car into a parking spot (array element)
  • You can write the lot number on a piece of paper (lot pointer, you can retrieve the car by giving that to the operator).
  • You cannot store a parking lot into another parking lot, they are not moveable.
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • so we can say that the array name values is a pointer which is a constant pointer. am i right? – sushant bandhekar Dec 03 '16 at 19:11
  • No, an array name is not a pointer. In the parking analogy, the piece of paper **is** the pointer. The name of the spot, or of the lot is the **value** of the pointer, an address. The array name is the address of the array, it can be used as a pointer **value**, and that's exactly what happens when you use an array in an expression (except as an argument to `sizeof`): the array *decays* to a pointer to its first entry. – chqrlie Dec 04 '16 at 09:52