Pointer and multidimensional arrays

This is a discussion on Pointer and multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Here is what I want to do: 1. I have two arrays - one with new data and one with ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    41

    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);
    }

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Now I can do it with the following code in the MC routine, but am concerned that the statement I have put in is actually copying the values in the array rather than effecting the pointers.

    [code]

    *ptrBeadArray = *&ptrBeadArrayOld;
    newptr = &ptrBeadArray;
    printf("%f\n",(*ptrBeadArray)[0][2]);
    //free_2d(*ptrBeadArray, 1, 3);
    free_2d(**newptr, 2, 3);

    [\code]

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    guess this is what you want to do
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    #include <stdio.h>
    
    
    void free_2d(double **d, int nx, int ny);
    
    void MC(double ***ptrBeadArray)  // you have to pass the array dimensions
    {
    	int i;
    	double **ptrBeadArrayOld = 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;
    
            double **oldPtr = *ptrBeadArray;
            
            free_2d(oldPtr, 2,3 );  // you don't know the dimensions here
                    
    	*ptrBeadArray = ptrBeadArrayOld;   // need to dereference
    }
    
    
    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;
            
            printf("%f\n",ptrBeadArray[0][0]);
            printf("%f\n",ptrBeadArray[0][1]);
            printf("%f\n",ptrBeadArray[0][2]);
    	
            MC(&ptrBeadArray);
    	
            printf("%f\n",ptrBeadArray[0][0]);
            printf("%f\n",ptrBeadArray[0][1]);
            printf("%f\n",ptrBeadArray[0][2]);
    	
    	free_2d(ptrBeadArray, 2, 3);
    	
    	return(0);
    }
    Kurt

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Thanks for that, but does the dereference copy the values over, in otherwords is there any copying of the elements?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Don't cast malloc() . . . .

    It does copy the data if you malloc() new memory for it. If you just set the pointer to the existing memory then it doesn't copy the data, just the pointer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Ok thanks for that....

    I have now done the re-pointing which has saved sometime. however I have some fixed arrays which are passed in the following way:

    Code:
    int (*ptrCellIndex)[Xsections][Ysections][Zsections][MaxBeadsinCell], int (*ptrCellIndexCount)[Xsections][Ysections][Zsections]
    I then have some static defined variables in the function. I wish to essentially copy one over to the other.

    Code:
    int CellIndexVolTest[Xsections][Ysections][Zsections][MaxBeadsinCell];
    int CellIndexEndVolTest[Xsections][Ysections][Zsections];
    What do you suggest I do for this? I need a faster way than just copying elements. The pointer route is'nt possible right? or is it?

    Thanks

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I wish to essentially copy one over to the other.
    That will use an incredible amount of time and memory if the dimensions of the array are bigger than, say, 10. Why do you need a copy of the array?

    I need a faster way than just copying elements. The pointer route is'nt possible right? or is it?
    You can use a pointer to access the original array, but if you intend to do something like modify the copy of the array, you would need to copy the array.

    The fastest way to copy an array would probably be to use memcpy().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 06:48 AM
  2. Multidimensional arrays and functions.
    By miniwhip in forum C Programming
    Replies: 6
    Last Post: 09-12-2007, 02:27 PM
  3. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 11:45 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21