D
D
dandropov952018-08-24 22:01:25
C++ / C#
dandropov95, 2018-08-24 22:01:25

Why can't an array name be "assigned" a new value?

For example, there is such a declaration and initialization of an array: It turns out that it points to the first element of a character array. it's just a pointer. Why can't its value be changed so that it points to a new address? And why can't you create arrays (non-character) in the style of pointers?
char arr[] = "string";
arrarr == &arr[0]
arr

int arr[] = {1, 2, 3, 4, 5}; // ok
int * arr = {1, 2, 3, 4, 5}; // ne ok

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2018-08-24
@jcmvbkbc

arrit's just a pointer.

No, arr is the name of an array. In C, pointers and arrays are different concepts.
Because it is not provided by the language in a context like yours. But in this context, you can:
void f(int arr[])
{
    arr = arr + 1;
}

From c99 onwards, you can:
int * arr = (int []){1, 2, 3, 4, 5}; // ok

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question