Stucture with array of pointers in C

What's wrong with this? Can I have an array of pointers to SDL_Surfaces in a struct in C?

typedef struct {
    int next_wheel;
    int pos_X;
    int pos_Y;
    int front_wheel_pos_X;
    int front_wheel_pos_Y;
    int velocity;
    int rear_wheel_pos_X;
    int rear_wheel_pos_Y;
    SDL_Surface* body;
    SDL_Surface* rear_wheel[9];
    SDL_Surface* front_wheel[9];
} mars_rover;


mars_rover* init_rover() {
    mars_rover* rover = (mars_rover*)malloc(sizeof(mars_rover) + sizeof(SDL_Surface) * 19);
    rover->body = load_image("Graphics//rover.png", ds_info);
    rover->front_wheel[0] = load_image("Graphics//wheel//wheel0.png", ds_info);

    return rover;

int main() {
    mars_rover* rover = init_rover();

EDIT: added my load image function.

SDL_Surface *load_image( const char* filename , SDL_VideoInfo* ds_info) {

    SDL_Surface* image = 0;
    SDL_Surface* converted_surface = 0;
    image = IMG_Load(filename);

    if(image) {
        converted_surface = SDL_DisplayFormatAlpha(image);

    return converted_surface;


You can, but you need to assign an allocated memory for each pointer separately. So instead of:

mars_rover* rover = (mars_rover*)malloc(sizeof(mars_rover) + sizeof(SDL_Surface) * 19);

you should have:

mars_rover* rover = malloc(sizeof(mars_rover));
rover->body = malloc(sizeof(SDL_Surface));
for (i = 0; i < 9; ++i) {
    rover->rear_wheel[i] = malloc(sizeof(SDL_Surface));
    rover->front_wheel[i] = malloc(sizeof(SDL_Surface));

Two notes though:

  1. You need to check the return value of each allocation
  2. You need to free each of the allocation when you clean up

Need Your Help

convert configuration file application.yml to application.groovy in Grails 3.x

grails grails-3.0

I am trying to create a simple Grails 3 project and got stuck with something really simple. So I want my data source properties to come from VM options that I set in my IntelliJ IDE. Before in Grails

MASM - Why doesn't this code cause the computer to beep?

assembly masm irvine32

I've read about the code to make the computer beep but I can't get it to work. The following code asks the user to enter digits. The digits are displayed on the screen but if the user types a non-d...