Pointer and multidimensional arrays
Here is what I want to do:
1. I have two arrays - one with new data and one with old. I effectively want to swap the pointer such that the pointer to the new data points at the old and the new data storage is deleted (i.e. no leaks).
2. I seem to be failing at the syntax stage. Tying up in Knots.
3. I dont wish to copy the elements..I need to save time, hence this pointer version...ideally which should just enavle me to swap between the two.
Thanks
Here is the code I am trying....its not working at it should as the changes are not being reflected.
#############################################
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
void MC(double ***ptrBeadArray);
void free_2d(double **d, int nx, int ny);
void MC(double ***ptrBeadArray)
{
int i;
double **ptrBeadArrayOld = NULL;
double ****newptr = NULL;
ptrBeadArrayOld = (double **)malloc(2 * sizeof(double *));
for (i = 0; i< 2; i++)
ptrBeadArrayOld[i] = (double *)malloc(3 * sizeof(double));
ptrBeadArrayOld[0][0] = 7.0;
ptrBeadArrayOld[0][1] = 7.0;
ptrBeadArrayOld[0][2] = 7.0;
ptrBeadArray = &ptrBeadArrayOld;
//newptr = &ptrBeadArray;
printf("%f\n",(*ptrBeadArray)[0][2]);
//free_2d(*ptrBeadArray, 1, 3);
//free_2d(**newptr, 2, 3);
}
void free_2d(double **d, int nx, int ny)
{
int x;
for (x = 0; x < nx; x++)
{
free(d[x]);
}
free(d);
}
int main(void)
{
int i;
double **ptrBeadArray = NULL;
ptrBeadArray = (double **)malloc(2 * sizeof(double *));
for (i = 0; i< 2; i++)
ptrBeadArray[i] = (double *)malloc(3 * sizeof(double));
ptrBeadArray[0][0] = 1.0;
ptrBeadArray[0][1] = 2.0;
ptrBeadArray[0][2] = 3.0;
MC(&ptrBeadArray);
printf("%f\n",ptrBeadArray[0][0]);
//free_2d(ptrBeadArray, 2, 3);
return(0);
}