How to assign values to an array of pointers in the C l…

In C language, you can assign values to an array of pointers in two ways:

  1. Assigning values to each element one by one.
int arr[] = {1, 2, 3, 4, 5};
int *ptrArr[5];

for (int i = 0; i < 5; i++) {
    ptrArr[i] = &arr[i];
}
  1. Use the memcpy function to copy the entire array at once.
int arr[] = {1, 2, 3, 4, 5};
int *ptrArr[5];

memcpy(ptrArr, arr, sizeof(arr));

Both methods can assign the pointer array ptrArr to point to the elements in the array arr.

bannerAds