When I want to initialize a pointer to an array through the function, I am doing the following:
Initialize and destroy array through functions:
int initArr(int **b)
{
int *arr = (int *) malloc(sizeof(int)*2);
if(arr == NULL)
return 0;
*b = arr;
arr = NULL;
return 1;
}
void destroyArr(int *b)
{
free(b);
b = NULL;
}
Initialize pointer to array:
int *pArr;
int initStatus = initArr(&pArr);
if(initStatus == 0)
{
printf("%s", "error");
return 0;
}
Working with pointer to array:
*pArr = 1;
*(pArr + 1) = 2;
printf("0 = %i\n", *pArr);
printf("1 = %i\n", *(pArr + 1));
Destroy pointer to array:
destroyArr(pArr);
pArr = NULL;
Is this correct and safe?
pArr[1] = 2;easier to read than your*(pArr + 1) = 2;. They are equivalent.