Thread: from 2D pointer to 1D array

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    from 2D pointer to 1D array

    Hi,

    I am trying to assign an array of pointer the values of a pointer to pointer, but canīt get it right.

    This is what I have :

    float **vars_array coming from the function arguments of size 3 x nnodes. Also nnodes comes as argument of the function.

    This is what I have now and is working properly:

    Code:
    void wrt2VTK_unstr(float **vars_array, int nnodes)
    {
    
    	unsigned int i;
    
    	float vars1[nnodes], vars2[nnodes], vars3[nnodes];
    
    	
    	//LOOP VARS:
    	for(i = 0; i<nnodes; i++){
    		vars1[i] = vars_array[i+1][0]; 
    		vars2[i] = vars_array[i+1][1]; 
    		vars3[i] = vars_array[i+1][2]; 
    
    	}
    	
    	//vars[]
    	float *vars[] = {vars1, vars2, vars3};
    
    return 0;
    }
    but what I need to do is, INSTEAD of assigning *vars[] with the arrays vars1, vars2, and vars3, I want to assign *vars[] directly with vars_array[1:nnodes][0], vars_array[1:nnodes][1], vars_array[1:nnodes][2].

    How can I pass these "2D" pointers to *vars[] ?

    thank you in advance,
    Best
    cfd

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, this isn't Fortran. vars_array[1:nnodes][0] is not a contiguous bunch of memory; vars_array[0][0:2] would be a contiguous bunch of memory and you could assign vars_array[0] to something if you wished. But you can't get to columns of a matrix, except as you have done.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi Tabstop,

    thank you for the information

    Best regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 2D char array and pointer
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-11-2009, 12:34 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. switch from 2d array to 1d array
    By sass208 in forum C Programming
    Replies: 12
    Last Post: 12-11-2006, 09:34 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM