assigning the value of a 2d matrix to an variable
<p>I have a 3x3 matrix which is not sorted.</p>

<p>After sorting the 3x3 matrix in ascending or descending order I want to store the median value in an integer variable (<code>int med</code>).</p>

<p>As my matrix is having odd number of elements I know that every time the median value will be <code>matrix[1][1]</code>, so I have basically written like this:</p>

<pre><code>int med = matrix[1][1];
</code></pre>

<p>Though I get no compile time error in Visual Studio while running the application for median, I got this below run time error:</p>

<pre><code>"#2 run time check failure..the stack around variable matrix[][] is corrupted..
</code></pre>

<p>Here is the code:</p>

<pre><code>#include "stdafx.h"
#include &lt;math.h&gt;
#include &lt;mtip.h&gt;
#define MAX 3

int medianfilter_MIF(HMTIPMOD hMod ,Dataset_byte *in,Dataset_byte **out,int th) 
{
    if(!in)
    {
        mmsg_ReportError("Missing input!");
        return MRV_FAILURE;
    }

    *out = (Dataset_byte*)md_DatasetClone((Dataset*)in, DST_SAME);
    if(!*out)
    {
        mmsg_ReportError("Could not clone image!");
        return MRV_FAILURE;
    }


    BYTE *pin = DATA_PTR(in);
    BYTE *pout = DATA_PTR(*out);
    int d_w = NX(in); 
    int d_h = NY(in);
    int mask2d_int[MAX][MAX];
    int *element;
    int temp;

    for (int i = 1; i &lt; d_w - 1; i++){  
        for (int j = 1; j &lt; d_h - 1; j++){
            int temp = VAL2(in,i,j);
            mmsg_ReportError("the value of temp is: %d ",temp);
            for (int p = i - 1; p &lt; i + 2; p++){
                for (int q = j - 1; q &lt; j + 2; q++){
                    mask2d_int[p][q]=VAL2(in,p,q);
                    mmsg_ReportError("the value of mask2d_int[p][q] : %d ",mask2d_int[p][q]);
                }
            }


    for (element = &amp;mask2d_int[0][0]; element &lt;= &amp;mask2d_int[MAX-1][MAX-1]; element ++) {
                 if (*element &gt; *(element + 1)) {
               *element ^= *(element + 1); /* exclusive or swap */
               *(element + 1) ^= *element;
               *element ^= *(element + 1);
               }
        msg_ReportError("the value of element after swapping is %d ",element);
            }
            int med = mask2d_int[1][1];
            mmsg_ReportError("the value of median is: %d ",med);
            if ((temp - med) &lt; th){
                *pout = temp;
                mmsg_ReportError("the value of *pout before is %d ",*pout);
                }
            else{
                *pout = med;
                mmsg_ReportError("the value of *pout after is %d ",*pout);
                }
            ++pout;
            }
        }
    return MRV_SUCCESS;
}


int medianfilter_MDF(HMTIPMOD hMod)
{
    INPUT_DATASET("Input image", 0, 0, DST_BYTE, CONN_REQUIRED);
    OUTPUT_DATASET("Output image", 0, 0, DST_BYTE, 0);
    CONTROL_INT_SLIDER("threshold:",30,0,255);
    return MRV_SUCCESS;
}

REGISTER_MTIP_MODULE("Median Filter", medianfilter_MDF, medianfilter_MIF, 0, 0, 0);
</code></pre>
