Home > Back-end >  Assign pointer to array
Assign pointer to array

Time:04-06

Is possible to assign pointer to array or array to array?

int arr[3] {1,2,3};
int pnt[3];
pnt = arr;
//or
pnt = &arr[0];

compiler don't want accept this....

CodePudding user response:

Is possible to assign pointer to array

Yes.

or array to array?

No. Arrays are not assignable.

compiler don't want accept this....

That's because pnt is not a pointer. You can create a pointer to the first element of the array like this:

int* pnt = arr;

CodePudding user response:

assign pointer to array

You can assign array to a pointer. E.g.:

int b[2] = {3,4};
int *p;
p=b;

Note, that in your case, you've tried to assign an array to another array, not a pointer. The variable p is still a pointer, but now happens to point to the place in memory, where the array b sits.

or array to array

No, you can't do this. You could use std::array if you want this kind of syntax. In case of the raw array, like in the example, you'd have to copy the data from one array to another.

  • Related