I do not know how to express the array I am going to work with, but some codes can describe them.

If I define an array and a struct with member :

float velocity;
MODEL.VELOCITY;

VELOCITY in struct MODEL is also float.
then I alocate memory for them;

velocity = malloc( NX*NY*NZ*sizeof(float); /* in 3D case, NX,NY,NZ are the length, here it is original velocity file*/
MODEL->VELOCITY = malloc(NX*NY*NZ*sizeof(float);

the question is that if I need to split MODEL->VELOCITY into layer-cake model, how can I read my original "velocity" file into MODEL->VELOCITY layer by layer.
I tried this method, but it is "segmentation fault":

for (i=0;i<N_subdomain_num; i++)
	         {
                 for (dy=0; dy < NY ; dy++)
                    for (dx=0; dx < NX; dx++)
		      {
		      if ( i == 0){
	                 for (dz=0; dz< domain[i].nz_comp; dz++)	/*domain[i].nz_comp is the depth of every layer, i from 0 to 10, and depths are different in each layer*/		
                           MODEL[i].VELOCITY[dz]= velocity[dz];                          
                     }
                      else	{	
               for (dz=domain[i-1].nz_comp; dz< domain[i].nz_comp; dz++)		           domain[i].velocity[dz1] = velocity1[dz1];                         
		      }
                 }

I donot know how to modify or other method can do it. If you have some other ideas, I am HIGHLY appriciated.

where is "MODEL" defined?

"MODEL" is defined in .h file, and every .c file includes this .h file. I didnot type in it because I think it is not the point. Am i right?

hmm this is the first time i am looking at a different way of allocating a 3D array. But referecing the elements within that is something really interesting. This is how i use to follow

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ***array;
    int i, j;
    
    if( ( array = malloc( sizeof( int **) * 10 ) ) != NULL )
    {
        for( i =0; i < 10; i++ )
        {
             if( ( array[i] = malloc( sizeof( int * ) * 10 ) ) != NULL )
             {
                 for( j = 0; j < 10 ; j++ )
                 {
                      if( ( array[i][j] = malloc( sizeof( int ) * 10 ) ) != NULL ) 
                          printf("a[ %d ][ %d ] allocated\n", i, j);
                 }
             }
        }
    }
    
    for( i = 0; i < 10; i++ )
         for( j = 0; j < 10; j++ )
              free( array[i][j] );
               
    for( i = 0; i < 10; i++ )
         free( array[i] );
    
    free( array );
    
    getchar();
    return 0;
}

ssharish

This article has been dead for over six months. Start a new discussion instead.