First of all, I need to thank all friends helping me here. Those days I learn a lot from your advices.
The basic idea of my code is to read velocity file into domain.velocity[k].
Start!!!

If I have a struct

typedef struct
{int nx, ny, nz;
  float *velocity, *pwave, *qwave;
  float *derivatives1, *derivatives2*;
 }Model;

Model Domain;

In my case, I have struct from Domain[0], Domain[1], Domain[2]... Domain. and in each Domain, I have Domain.velocity[k], Domain.pwave[k], domain.qwave[k],....k = nx*ny*nz. All I did is in 3D case, that why I have nx*ny*nz.

How can I allocate memory for them? Do I need to allocate dynamic memory for them? How to do it?

I am sure "Segmentation fault" come from allocation memory, but I have no idea to figure it out. Hope you guys can give me a clear direction. Thanks in advance.

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

typedef struct
{
     int *array;
} MODEL;

int main()
{
    MODEL *node;
    int index;
    
    srand( time(0) );
    
    if( ( node = malloc( sizeof( MODEL ) ) ) == NULL )
    {
        perror("malloc");
        return 1;
    }
    else
    {
        printd("1. Node creation successful\n");
        
        if( ( node->array = malloc( sizeof( int ) * 10 ) ) == NULL )
        {
            perror("malloc-array");
            return 1;
        }
        else
        {
            printf("2. Node-array creation successfull\n");
            
            for( index = 0; index < 10; index++ )
                 node->array[index] = rand() % 10;
            
            printf("3. Node-array is filled with random values \n");
            
            printf("4. Print node-array\n");
            
            for( index = 0; index < 10; index++ )
                 printf("%d\t", node->array[index] );
            
            free( node->array );
            printf("5. Node-array is freed\n");
        }
        
        free( node );
        printf("6. node is freed\n");
    }
    
    getchar();
    return 0;
}

This is just a sample code, please not i dint compile this code. I did my level best. Since my computer is totally froze, I couldn't compile. Hope that shows and idea on how to allocate memory for your structure pointer and more importantly you will have to free them.

ssharish.

Thank you for your kind help.

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

typedef struct
{ 
    int *array; 
} MODEL;

int main()
{  
   MODEL *node;
   
   node = malloc( 10 * sizeof( MODEL ) ) ;  /* I have node[10] */
       
   node->array = malloc( 100 * sizeof(int) ) ; /*in array, it includes 100 values*/

   /* problem is here:
      if I set node->array: warning: assignment makes integer from pointer without a cast

      if I set node.array: error: request for member ‘array' in something not a structure or union
      What do they mean? 
   */
   
   free( node->array );
   printf("5. Node-array is freed\n");
       
   free( node );
   printf("6. node is freed\n");
   
   getchar();
   return 0;
}

/* my output
[ccccccc@outcast ~/CWork]$ ./a.out 
5. Node-array is freed
6. node is freed
*/

>if I set node->array: warning: assignment makes integer from pointer without a cast
Before, I answer this question, what compiler are you using. It looks like that your using a very old compiler. Well, its telling you to type cast the return type of the malloc. Which it shouldn't. Cos the malloc returned void * which is a generic pointer which get types cast internally. And its not a good programming pratice to type cast the malloc.

The always make sure that you check the return value of malloc. That is MUST.

>if I set node.array: error: request for member ‘array' in something not a structure or union
What this is saying is that, the struct MODEL donst has a data member called array. Well, for me it looks like it is on my code. I can really comment on this unless i can see your .h file. So that i could see what changes have yopu made. And made the .h should be on the local directory on which your .c file is. If its in a different dic, you could still refer to that by absolute path like:

#include "CCode/mycode/MODEL.h"

assuming that the MODEL.h file in locate under mycode folder.

Hope that gives you more help.

ssharish

This post refered to my first post which i did on this thread. I posted a code which wasn't compiled. But here is the proper working code.

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

typedef struct
{
     int *array;
} MODEL;

int main()
{
    MODEL *node;
    int index;
    
    srand( time(0) );
    
    if( ( node = malloc( sizeof( MODEL ) ) ) == NULL )
    {
        perror("malloc");
        return 1;
    }
    else
    {
        printf("1. Node creation successful\n");
        
        if( ( node->array = malloc( sizeof( int ) * 10 ) ) == NULL )
        {
            perror("malloc-array");
            return 1;
        }
        else
        {
            printf("2. Node-array creation successfull\n");
            
            for( index = 0; index < 10; index++ )
                 node->array[index] = rand() % 10;
            
            printf("3. Node-array is filled with random values \n");
            
            printf("4. Print node-array\n");
            
            for( index = 0; index < 10; index++ )
                 printf("%d\t", node->array[index] );
            
            free( node->array );
            printf("5. Node-array is freed\n");
        }
        
        free( node );
        printf("6. node is freed\n");
    }
    
    getchar();
    return 0;
}

/* my output
1. Node creation successful
2. Node-array creation successfull
3. Node-array is filled with random values
4. Print node-array
9       4       9       8       0       4       1       4       9       9
5. Node-array is freed
6. node is freed
*/

ssharish

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


#include <errno.h>
#include <time.h>

#include "/home/mydir/inc/struct..h"

/* I also define struct in other file "struct.h" in /home/mydir/inc/struct.h,and delete struct here*/
/*typedef struct   
{                 
    int *array; 
} MODEL;
*/


void a(MODEL *NODE, int t);  /* if I add new function */

int main()
{  int i;
   MODEL *node;
   
   node = malloc( 10 * sizeof( MODEL ) ) ;  /* I have node[10] */
       
   node->array = malloc( 100 * sizeof(int) ) ; /*in array, it includes 100 values*/

   /* OK, it compile successfully this time  */
 for (i=0:I<10; i++)
   a(node, i);
 
   return 0;
}


/* function a() in other file, we call it additional.c */

#include "/home/mydir/inc/struct.h"
void a(MODEL *NODE, int t)
{int j,k;
  for (j=0; j<100;j++)
   NODE[t].array[j]=j+1;

 /* if I set NODE[t].array, it can be compiled successfully,
    if I set NODE[t]->array, it shows: "error: invalid type argument of ‘->’".  I think I should use pointer operator "->" because I call *NODE, but why it falis. I also set absolute path of .h for every file and I use gcc in ubuntu*/

return;
}

ssharish

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.