Dynamicly allocating an array of pointers

This is a discussion on Dynamicly allocating an array of pointers within the C Programming forums, part of the General Programming Boards category; I have been having a spot of trouble making a dynamic array of pointers. Sanimation.sprite should be an array of ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Dynamicly allocating an array of pointers

    I have been having a spot of trouble making a dynamic array of pointers. Sanimation.sprite should be an array of sprite pointers. I am getting an "Incompatible types in assignment" error on the "new_animation->sprite[i] = ..." line. Because theres quite alot of irrelevant stuff I have pasted sniplets that should be enough to explain what I'm doing.

    Code:
    /* Animation structure */
    struct Sanimation
    {
        struct Ssprite *sprite; /* Pointer to the array of sprites */
        int count; /* How many sprites in this animation ? */
    };   
    
        new_animation = malloc(sizeof(struct Sanimation));
        new_animation->sprite = malloc(sizeof(struct Ssprite*) * spritecount);
    
    new_animation->sprite[i] = load_sprite(filename,x,y,width,height,x_offset,y_offset);
    
    struct Ssprite* load_sprite(char *file_name, int x, int y, int width, int height, int x_offset, int y_offset)
    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    hey look there,

    Code:
     struct Ssprite *sprite;
    you are declaring just a array of type struct Ssprite not the array of pointer of type struct Ssprite.

    and in the bottom of your code
    Code:
    new_animation->sprite[i] = load_sprite(filename,x,y,width,height,x_offset,y_o  ffset);
    
    struct Ssprite* load_sprite(char *file_name, int x, int y, int width, int height, int x_offset, int y_offset)
    u are assiging a pointer to an array of struct Ssprite.which is not correct , the array should be a pointer to pointer of type struct Ssprite

    Code:
    struct Ssprite *sprite[10];  or **sprite if dynamic memory allocation
    s.s.harish

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    114
    Oh woops. I tried changing it to **Sprite before and I got an error about making a pointer to an integer withought a cast so I assumed I had the wrong idea. After you suggested it I tried it again and realised the error was because I hadn't declared the function at the top, so it defaulted to returning an integer and messed everything up. Anyway its working now Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21