C 试图将指针复制到数组中

C 试图将指针复制到数组中,c,pointers,C,Pointers,我一直在尝试的一些代码中出现错误,我不确定为什么。请看下面(我已将所需的功能拉入一个窗口,这些功能实际上包含在3个不同的文件中): 在更多的背景中,粒子在另一个函数中被初始化,我试图查看哪些粒子落在某个特定容器的区域中,并将落在该容器中的粒子添加到该容器中,将它们存储在一个数组中。我不认为我掌握了正确地将粒子推入阵列的想法。。。我希望以上内容能让您很好地了解我正在尝试做什么,而不需要太多额外的东西。错误表明b[j]。p实际上没有初始化。因此问题可能出在创建垃圾箱的代码中。这是一个输入错误:for

我一直在尝试的一些代码中出现错误,我不确定为什么。请看下面(我已将所需的功能拉入一个窗口,这些功能实际上包含在3个不同的文件中):


在更多的背景中,粒子在另一个函数中被初始化,我试图查看哪些粒子落在某个特定容器的区域中,并将落在该容器中的粒子添加到该容器中,将它们存储在一个数组中。我不认为我掌握了正确地将粒子推入阵列的想法。。。我希望以上内容能让您很好地了解我正在尝试做什么,而不需要太多额外的东西。

错误表明
b[j]。p
实际上没有初始化。因此问题可能出在创建垃圾箱的代码中。这是一个输入错误:
for(int j=0;j
。这应该是
j++
。在调试器中,您可以查看
i
j
的值。错误表明
b[j]。p
实际上没有初始化。因此问题可能出在创建容器的代码中。这是一个输入错误:
for(int j=0;j
。这应该是
j++
。在调试器中,您可以查看
i
j
的值。
#define density 0.0005 

const int NBINS = 10;

double size;

typedef struct 
{
   int x_start;
   int x_end;
   int y_start;
   int y_end;
   particle_t* p;
   bool left;
   bool right;
   bool up;
   bool down;
} bins;

typedef struct 
{
  double x;
  double y;
  double vx;
  double vy;
  double ax;
  double ay;
} particle_t;

int find_option( int argc, char **argv, const char *option )
{
    for( int i = 1; i < argc; i++ )
        if( strcmp( argv[i], option ) == 0 )
            return i;
    return -1;
}

int read_int( int argc, char **argv, const char *option, int default_value )
{
    int iplace = find_option( argc, argv, option );
    if( iplace >= 0 && iplace < argc-1 )
        return atoi( argv[iplace+1] );
    return default_value;
}

void set_size( int n ) {
    size = sqrt( density * n );
}

void init_particles( int n, particle_t *p ) {
    srand48( time( NULL ) );

    int sx = (int)ceil(sqrt((double)n));
    int sy = (n+sx-1)/sx;

    int *shuffle = malloc( n * sizeof(int) );
    for( int i = 0; i < n; i++ )
        shuffle[i] = i;

    for( int i = 0; i < n; i++ ) 
    {
        int j = lrand48()%(n-i);
        int k = shuffle[j];
        shuffle[j] = shuffle[n-i-1];

        p[i].x = size*(1.+(k%sx))/(1+sx);
        p[i].y = size*(1.+(k/sx))/(1+sy);

        p[i].vx = drand48()*2-1;
        p[i].vy = drand48()*2-1;
    }
    free( shuffle );
}

void create_bins(int n, int num_bins, bins* b) {
   int x = 0;
   int y = 0;

   for (int i = 0; i < num_bins; i++) {
      b[i].x_start = x; 
      b[i].x_end = x + NBINS; 
      x += NBINS;

      b[i].y_start = y; 
      b[i].y_end = y + NBINS; 
      y += NBINS;

      b[i].p = malloc(n * sizeof(particle_t));
   }
}

void add_to_bin (int n, int num_bins, bins* b, particle_t* p) {
   for (int i = 0; i < n; i++)
       for (int j = 0; j < num_bins; i++)
          if (p[i].x >= b[j].x_start && p[i].x < b[j].x_end)    // Check if particle is within bin's x boundaries
             if(p[i].y >= b[j].y_start && p[i].y < b[j].y_end) {  // Check if particle is within bin's y boundaries
                b[j].p[0] = p[i];
             }
}

int main( int argc, char **argv )
{
   if( find_option( argc, argv, "-h" ) >= 0 )
   {
      printf( "Options:\n" );
      printf( "-h to see this help\n" );
      printf( "-n <int> to set the number of particles\n" );
      printf( "-o <filename> to specify the output file name\n" );
      printf( "-s <filename> to specify a summary file name\n" );
      printf( "-no turns off all correctness checks and particle output\n");
      return 0;
   }

   int n = read_int( argc, argv, "-n", 1000 );

   int num_bins = n / NBINS;

   bins* b = malloc (num_bins * sizeof(bins));

   create_bins(n,num_bins,b);

   particle_t *particles = malloc( n * sizeof(particle_t) );
   set_size( n );
   init_particles( n, particles );

   add_to_bin(n,num_bins,b,particles);

   return 0;
}
(gdb) run
Starting program: /workspace/particle/serial

Program received signal SIGSEGV, Segmentation fault.
0x0000000008001101 in add_to_bin (n=1000, num_bins=100, b=0x8415e70, p=<optimized out>) at bins.h:70
70                                      b[j].p[0] = p[i];