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:
The struct:
typedef struct ball{
BITMAP *image;
int x;
int y;
int vector_x;
int vector_y;
} ball;
The (non working function):
void add_balls(int *num_balls, ball **myballs){
num_balls++;
*myballs = realloc(*myballs, *num_balls * sizeof(ball));
*myballs[*num_balls-1]->x = rand() % 640;
*myballs[*num_balls-1]->y = rand() % 480;
*myballs[*num_balls-1]->vector_x = rand() % 10;
*myballs[*num_balls-1]->vector_y = rand() % 10;
*myballs[*num_balls-1]->image = load_bitmap("blue_ball.bmp", NULL);
}
and the function call within main:
add_balls(&num_balls, &myballs);
The call to the function fails with the following error messages:
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
Any help?
That helped. It compiles now, but I get a segmentation fault error in runtime. Here is the complete code, if it interests:
#include <allegro.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#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]->x = rand() % 640;
myballs[*num_balls-1]->y = rand() % 480;
myballs[*num_balls-1]->vector_x = rand() % 10;
myballs[*num_balls-1]->vector_y = rand() % 10;
myballs[*num_balls-1]->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<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<num_balls;i++){
if(myballs[i].x + myballs[i].vector_x > 640 || myballs[i].x + myballs[i].vector_x < 0){
myballs[i].vector_x *= -1;
}
if(myballs[i].y + myballs[i].vector_y > 480 || myballs[i].y + myballs[i].vector_y < 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(&num_balls, &myballs);
}
clear_bitmap(buffer);
int ii;
for(i=0;i<3;i++){
for(ii=-1;ii<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<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<num_balls;i++){
destroy_bitmap(myballs[i].image);
}
free(myballs);
destroy_bitmap(buffer);
}
END_OF_MAIN()