passing an array created with malloc as a param

This is a discussion on passing an array created with malloc as a param within the C Programming forums, part of the General Programming Boards category; Hi folks, i want to know how can i pass an array wich has been created with malloc() as a ...

With this way, it works fine. But i have a serious doubt. When i create my array, i'm determining that it will be a 10x6 matrix. But when i set as a param i'm determining that it will have 1x1 elements. So, what are the secondary effects that might appear with this code?

You always have to pass the (correct!) second (and later) sizes of an array when you pass an array as a parameter. You should get errors, or at least warnings, about the first too -- you're not actually passing a 0-size array, and I don't even think that means anything.

Passing a 0-dimension array will cause errors in your code since the compiler will use the known column dimension to calculate the correct offset when storing and retrieving data.
If an array is 5x5, and you access [3][4], the actual offset is 3 x 5 + (4 + 1), so 3 x 5 + 5 = 20.
But if you pass that as an array with a lesser column dimension, such as [][0], you would get this when accessing [3][4]:
3 x 1 + (4 + 1) = 3 x 1 + 5 = 8!
So it writes (or reads) to/from the incorrect memory location. That's why you need to pass correct size.

Disclaimer: The post may be subject to some small errors, but the general idea is correct. I reserve the right to be corrected.

I have a new problem about it. I declare a var of type rgb_pixel (*) [size]. I have a structure named bmp_image wich one of its member needs to be a var of the type above declared. But size , is not an arbitrary number, it depends on bitmap image height. So, i try to do the same as i did with functions, this is to use a param of the same function to retrieves the image height, just like this:

I have thought about a solution, wich i'm implementing by now, but i have a doubt.
I define a const named MAXIMAGEHEIGHT, so in my structure i declare the array with this arbitrary number (set to 2048). When i allocate memory for my array i do the following:

Here, hdrs.header.imageHeight will always be minor or equal to MAXIMAGEHEIGHT. I have a doubt about what does happen in last line if it's minor than const. Will img.RGBPixels be automatically reallocated, changing its size to necessary number of bytes?. Do i have to reallocate it myself to assure me that my array only occupies memory to storage hdrs.header.imageHeight elements and not MAXIMAGEHEIGHT?. I hope you can save my doubt.

Can you kindly post the names of the header files you are including?
The declaration of the struct, is wrong, wrong, wrong.
You are assuming that there is an object, named 'headers' with initialized at compile time values, which even if there was one you probably need different imageHeight values for every bmp_image you declare. Also the call to readBmpSingleSampleImageArray is definately wrong. The third argument cannot have (1) two identifier components, (2) if it is an array of constant length, it should be declared in the header file of the function, and not acquired through run-time data, or else it is a pointer.

Thanks for replying.
About the call of readBmpSingleSampleImageArray call, the third argument is this:

singleSample_pixel(*array)[hdrs.header.imageHeight]

In green i declare param's type. It's singleSample_pixel. In blue, i'm declaring an identifier <array>, wich is a pointer to singleSample_pixel. In red, i'm determining 2nd dimention's array's size. This type is the exact type i use to allocate memory for the array. My application works fine with it, and doesn't throw any warning or error about it. Is it wrong to do it like this? even C compiler accepts it, is there a chance that in some cases my application don't work?.
About second thing you remark, just like you said, it's a number wich its value depends on the bitmap image, and i cannot declare an array of constant length.

You simply can't define an array on the stack (or arguments) with a runtime filled variable. There is an exception in C99, but for local variables. For dynamic size, you need a pointer or if you need a 2D-array, you need a pointer-to-pointer, **.

How i would do that?, i can't get the idea of it. If a need a pointer-to-pointer param, does it mean that i have to change all var's type as the one i said above? (rgb_pixel (*)[size]). Because i just tried to do it, changing only param's type to pointer-to-pointer, but it says that i'm passing an arg from incompatible pointer type. So how do i do what you said elysia?. Apologizes my ignorance, i never used pointers-to-pointers vars.

I was looking for it around the net, and i should change the way i'm declaring vars. The only way i knew how declaring a 2D array type was array_type (*array)[size]. But in C a 2D array is an array of arrays, so it's a pointer of pointers, and that's (i think) a better way of seeing it.
Thanks.