Note that there are some explanatory texts on larger screens.

plurals
  1. POrealloc from within a function
    text
    copied!<p>I have a struct called ball, a number of balls, an array of balls and a function where I want to add new balls to the array:</p> <p>The struct:</p> <pre><code>typedef struct ball{ BITMAP *image; int x; int y; int vector_x; int vector_y; } ball; </code></pre> <p>The (<em>non working</em> function):</p> <pre><code>void add_balls(int *num_balls, ball **myballs){ num_balls++; *myballs = realloc(*myballs, *num_balls * sizeof(ball)); *myballs[*num_balls-1]-&gt;x = rand() % 640; *myballs[*num_balls-1]-&gt;y = rand() % 480; *myballs[*num_balls-1]-&gt;vector_x = rand() % 10; *myballs[*num_balls-1]-&gt;vector_y = rand() % 10; *myballs[*num_balls-1]-&gt;image = load_bitmap("blue_ball.bmp", NULL); } </code></pre> <p>and the function call within main:</p> <pre><code>add_balls(&amp;num_balls, &amp;myballs); </code></pre> <p>The call to the function fails with the following error messages:</p> <pre><code>p.c: In function ‘add_balls’: p.c:19: error: invalid type argument of ‘unary *’ p.c:20: error: invalid type argument of ‘unary *’ p.c:21: error: invalid type argument of ‘unary *’ p.c:22: error: invalid type argument of ‘unary *’ p.c:23: error: incompatible types in assignment </code></pre> <p>Any help?</p> <p>That helped. It compiles now, but I get a segmentation fault error in runtime. Here is the complete code, if it interests:</p> <pre><code>#include &lt;allegro.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;math.h&gt; #define NUM_BALLS 10 typedef struct ball{ BITMAP *image; int x; int y; int vector_x; int vector_y; } ball; void add_balls(int *num_balls, ball **myballs){ num_balls++; myballs = realloc(*myballs, *num_balls * sizeof(ball)); myballs[*num_balls-1]-&gt;x = rand() % 640; myballs[*num_balls-1]-&gt;y = rand() % 480; myballs[*num_balls-1]-&gt;vector_x = rand() % 10; myballs[*num_balls-1]-&gt;vector_y = rand() % 10; myballs[*num_balls-1]-&gt;image = load_bitmap("blue_ball.bmp", NULL); } int main() { allegro_init(); install_keyboard(); srand(time(0)); install_timer(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0); BITMAP *buffer = NULL; buffer = create_bitmap(640,480); ball *myballs; myballs = malloc(NUM_BALLS * sizeof(ball)); int num_balls = NUM_BALLS; BITMAP *bg = NULL; bg = load_bitmap("bg.bmp",NULL); int i; for(i=0;i&lt;num_balls;i++){ myballs[i].x = rand() % 640; myballs[i].y = rand() % 480; myballs[i].vector_x = rand() % 10; myballs[i].vector_y = rand() % 10; myballs[i].image = load_bitmap("blue_ball.bmp", NULL); } int bg_vector_x; float bg_vector_y; while(!key[KEY_ESC]){ vsync(); for(i=0;i&lt;num_balls;i++){ if(myballs[i].x + myballs[i].vector_x &gt; 640 || myballs[i].x + myballs[i].vector_x &lt; 0){ myballs[i].vector_x *= -1; } if(myballs[i].y + myballs[i].vector_y &gt; 480 || myballs[i].y + myballs[i].vector_y &lt; 0){ myballs[i].vector_y *= -1; } myballs[i].x += myballs[i].vector_x; myballs[i].y += myballs[i].vector_y; } if(key[KEY_UP]){ add_balls(&amp;num_balls, &amp;myballs); } clear_bitmap(buffer); int ii; for(i=0;i&lt;3;i++){ for(ii=-1;ii&lt;3;ii++){ draw_sprite(buffer, bg ,(bg_vector_x%528)+(i*528),100*cos(bg_vector_y) + ii*353); } } bg_vector_x++; bg_vector_y+=0.1; for(i=0;i&lt;num_balls;i++){ draw_sprite(buffer, myballs[i].image, myballs[i].x,myballs[i].y); } blit(buffer, screen, 0,0,0,0,640,480); } for(i=0;i&lt;num_balls;i++){ destroy_bitmap(myballs[i].image); } free(myballs); destroy_bitmap(buffer); } END_OF_MAIN() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload