Dynamic Structure Array Help

This is a discussion on Dynamic Structure Array Help within the C Programming forums, part of the General Programming Boards category; I need to dynamically create this structure array, at the beginning of my program and when the window is updated. ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Dynamic Structure Array Help

    I need to dynamically create this structure array, at the beginning of my program and when the window is updated.

    I am using GLUT (OpenGL) to create a gradient fill image and draw it's pixels on the screen, and I need the image to update itself when the window is resized.

    Does anyone know how to get it working with malloc to dynamically create the array? Thanks in advance.

    Code:
    /* my image structure */
    typedef struct tagColour{
    	float r; /* red   */
    	float g; /* green */
    	float b; /* blue  */ 
    } PIXEL_COLOUR;
    
    PIXEL_COLOUR pixel_image[PIXEL_WIDTH][PIXEL_HEIGHT];

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to use a pointer to a pointer instead, assuming you want a dynamic "2D array".
    Code:
    something **foo;
    
    foo = malloc( sizeof( something* ) * num_of_rows );
    for( x = 0; x < num_of_columns; x++ )
        foo[x] = malloc( sizeof( something ) * num_of_columns );
    Be sure and free each column and row correctly when you're done.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Can this code be used in the image[x][y] structure format?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Just figured out another solution to my code problem. Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Originally posted by quzah
    You'll need to use a pointer to a pointer instead, assuming you want a dynamic "2D array".
    Code:
    something **foo;
    
    foo = malloc( sizeof( something* ) * num_of_rows );
    for( x = 0; x < num_of_columns; x++ )
        foo[x] = malloc( sizeof( something ) * num_of_columns );
    Be sure and free each column and row correctly when you're done.

    Quzah.
    I ran into another problem last night, and I want to copy my image data (which is in the format image[x][y] into a temp image, but it needs to be dynamic because the image size is always changing) to a temp_image structure. (Note: image[x][y] is a structure with three float values for RGB data).

    Will the above example work in the context temp_image[x][y] ?

    If not, does anyone know a way?

    Thanks again.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by B_D_C
    Will the above example work in the context temp_image[x][y] ?
    Yes.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Originally posted by quzah
    Yes.

    Quzah.
    So that initializes both the first and second elements of the array?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by B_D_C
    So that initializes both the first and second elements of the array?
    Try it and find out. I've already answered.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Originally posted by quzah
    Try it and find out. I've already answered.

    Quzah.
    Yeah, I tried it out yesterday and it works.

    However, I found that I didn't even need to use 2D dynamic array. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Creating Dynamic Structure in C
    By SomeNath in forum C Programming
    Replies: 1
    Last Post: 03-19-2005, 08:11 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

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