4 Replies - 749 Views - Last Post: 25 January 2016 - 09:01 PM Rate Topic: -----

#1 rshah   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 21-October 15

Can't use fixed array in 'dynamic array' in struct

Posted 25 January 2016 - 02:25 PM

So I have a dynamic array in my struct Image:

struct Pixel {
    int r;
    int g;
    int b;
} Pixel;

struct Image {
    struct Pixel **rgb;
} Image;


Now when i come to use it, I create a fixed array (I know the width and height before coming to the pixels within the image). Here is my code for creating the fixed array:
struct Pixel rgbArray[HEIGHT][WIDTH];


But when I set the array in the struct to this new array:
myStruct->rgb = rgbArray;


I get the following warning:

Quote

warning: assignment from incompatible pointer type [enabled by default]

Because my program crashes when I run it to print the values in my array:
int x, y;
for(y = 0; y < HEIGHT; y++) {
    for(x = 0; x < WIDTH; x++) {
        struct Pixel newPixel = myStruct->rgb[y][x];
        printf("%d %d %d\n", newPixel.r, newPixel.g, newPixel.B)/>/>;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Can't use fixed array in 'dynamic array' in struct

#2 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Can't use fixed array in 'dynamic array' in struct

Posted 25 January 2016 - 02:46 PM

Please show the smallest possible complete program that illustrates your problem. But remember you can't use assignment with arrays.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 rshah   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 21-October 15

Re: Can't use fixed array in 'dynamic array' in struct

Posted 25 January 2016 - 02:51 PM

Here is my program:

http://pastebin.com/jzGj5D6B

Here is the sample file input i use:

http://pastebin.com/k5TzqriZ
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Can't use fixed array in 'dynamic array' in struct

Posted 25 January 2016 - 08:56 PM

This is a classic of example of "pointers are not arrays, and array are not pointers". A double pointer is a pointer to a pointer, where a 2D array is a sequential block of memory. You cannot assign a 2D array to a double pointer, it doesn't make sense.

You can allocate memory for your 2D pointer, and adjust your pointers to point at the beginning of each row of the 2D array. Or you can just use a single pointer and map your 2D indexs to 1D indexs

i.e.
#define INDEX_1D(r, c) ((r) * WIDTH + c)

struct Image {
    struct Pixel *rgb;
} Image;



myStruct->rgb = rgbArray;

// then access elements like so
myStruct->rgb[INDEX_1D(2, 3)] = 1; // Sets element at row 1, column 3 to 1



But since you already know how big your image is, defined by WIDTH x HEIGHT. Why not just allocate your memory before hand without using pointers?

struct Image {
    struct Pixel rgb[HEIGHT][WIDTH];
} Image;


This post has been edited by jjl: 25 January 2016 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Can't use fixed array in 'dynamic array' in struct

Posted 25 January 2016 - 09:01 PM

If you were using "normal" dynamic memory instead of the VLA in the following snippet you would probably be better off, just don't forget to free the memory when you're finished with it.

    //new array of width rows, height columns
    struct Pixel rgbArray[newPPMFile->height][newPPMFile->widt

This is a Variable Length Array which is only guaranteed to work with the C99 version of the standard, you should probably be using malloc()/free instead.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1