Question: I have the following method to read the colors from a PPM file:

```
??? getColors(FILE *fd, int width, int height){

    COLOR (*colors)[width] = malloc( sizeof(COLOR[height][width]) );

    int i,j;
    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++) {        
            fscanf(fd,"%d %d %d", &colors[i][j].red, &colors[i][j].green, &colors[i][j].blue);  
        }
    }

    return colors;
}

```

where fd is the file, with the pointer pointing at the first color, and width and height are the width and height of the PPM file. What is the type I return?

EDIT:

Just so I understand, I'm returning a pointer 1d array that is a size of a 2d array correct?

so 

```
COLOR (*getColors(FILE *fd, int width, int height))[]{

    COLOR (*colors)[width] = malloc( sizeof(COLOR[height][width]) );

    int i,j;
    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++) {        
            fscanf(fd,"%d %d %d", &colors[i][j].red, &colors[i][j].green, &colors[i][j].blue);  
        }
    }

    return colors;
}

```

is what I am returning and Im using it like this:

```
COLOR *colorValues[];

colorValues = getColors(fd, width, height);

```

right?

Answer: 
The function signature in your "edit" is correct. You could use a typedef here. Normally I would advise against array typedefs, however it is hard to read when a function returns a pointer to an array because of how some of the return type comes after the parameter list. For example:

```
typedef COLOR ColorArray[];

ColorArray *getColors(FILE *fd, int width, int height) {

```

The usage should be:

```
COLOR (*colorValues)[width] = getColors(fd, width, height);

```

Although you *can* omit `width` in the declaration, it would mean that you could not then go on to write `colorValues[a][b]` later. You'd have to use a cast or later assign to a pointer to array with that dimension filled in.
In C, an array is treated as a pointer when it is passed or returned. 

```
COLOR *c

```

and

```
COLOR c[]

```

are treated in the same way. The act of accessing an element of an array is the same as dereferencing a pointer.

```
c[i]

```

is understood by the compiler to mean

```
*(c+i)

```

Array access is dereferencing the value that is stored at the memory address `c + i *sizeof(c)`.

When you allocated memory with `malloc( sizeof(COLOR[height][width]) );` you created an array of arrays. Another way to think of this object is the pointer to the the memory address where many more pointers are stored. Each one of those pointers points to an array. So the objects \*\*COLOR, COLOR[][], and \*COLOR [] all represent the same thing, and can be used in the same way. Technically there are slight differences, but I won't mention them here.

Additionally, it is often more efficient to store 2D data in a one dimensional array. This is because moving around in a single block of memory is more efficient than moving between blocks.

so instead of 

```
COLOR (*colors)[width] = malloc( sizeof(COLOR[height][width]) );
color[row][column]  

```

you could use

```
COLOR *colors = malloc(width*height*sizeof(COLOR);
color[row*numcolumns+column]

```