C 如何修复未处理的异常访问冲突

C 如何修复未处理的异常访问冲突,c,pointers,C,Pointers,我编写了一个逻辑来从两个数组中找出公共元素。但程序在if条件下中断,给出一个异常,称为访问冲突读取位置0x00000002 #include<stdio.h> void intersect(int[2][2],int[2][2],int,int); int main() { int arr1[2][2]={{2,5},{6,8}}; int arr2[2][2]={{1,2},{8,8}}; row = (sizeof(arr1)/sizeof(arr1[0]));

我编写了一个逻辑来从两个数组中找出公共元素。但程序在if条件下中断,给出一个异常,称为访问冲突读取位置0x00000002

#include<stdio.h>
void intersect(int[2][2],int[2][2],int,int);
int main()
{
    int arr1[2][2]={{2,5},{6,8}};
    int arr2[2][2]={{1,2},{8,8}};
row = (sizeof(arr1)/sizeof(arr1[0]));
     col = (sizeof(arr1[0])/sizeof(arr1[0][1]));
intersect(arr1,arr2,row,col);
}

void intersect(int **ptr1, int **ptr2,int row, int col)
{
    int i = 0, j= 0, x = 0, y = 0;

    for(i = 0; i <row ; i++)
    {
        for(j = 0 ; j < col ; j++)
        {
                for(x = 0; x < row ; x++)
                {
                    for(y = 0; y < col ; y++)
                    {
                        if(ptr1[i][j] == ptr2[x][y]) 
                            printf("%d\t",ptr1[i][j]);

                    }
                }
        }
    }
}
#包括
虚空相交(int[2][2],int[2][2],int,int);
int main()
{
int arr1[2][2]={{2,5},{6,8};
int arr2[2][2]={{1,2},{8,8};
行=(sizeof(arr1)/sizeof(arr1[0]);
col=(sizeof(arr1[0])/sizeof(arr1[0][1]);
相交(arr1、arr2、行、列);
}
无效相交(int**ptr1、int**ptr2、int行、int列)
{
int i=0,j=0,x=0,y=0;

对于(i=0;i而不是使用
ptrN
作为双指针,您可以告诉数组大小如下:

void intersect(size_t row, size_t col, int a1[][col], int a2[][col])
{
    size_t i = 0, j= 0, x = 0, y = 0;

    for(i = 0; i <row ; i++)
    {
        for(j = 0 ; j < col ; j++)
        {
            for(x = 0; x < row ; x++)
            {
                for(y = 0; y < col ; y++)
                {
                    if(a1[i][j] == a2[x][y]) 
                        printf("%d\t",a1[i][j]);
                }
            }
        }
    }
}
void intersect(大小行、大小列、int a1[][col]、int a2[][col])
{
尺寸i=0,j=0,x=0,y=0;

对于(i=0;i当您将
2D array
作为函数的参数传递时,您应该使用
指向数组的指针来捕捉,而不是使用双指针,因为2D array和双指针不相同

    #define r 2
    #define c 2


    void intersect(int (*ptr1)[r], int (*ptr2)[c],int row, int col)
    {
           //function body
    }

这是有效的…不确定这是否是你想要的

void intersect(int** ptr1, int** ptr2, size_t row, size_t col)
{
int i = 0, j = 0, x = 0, y = 0;

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        for (x = 0; x < row; x++)
        {
            for (y = 0; y < col; y++)
            {
                int a = *(int*)((DWORD)ptr1 + (i * (col * sizeof(int))) + (j * sizeof(int)));
                int b = *(int*)((DWORD)ptr2 + (x * (col * sizeof(int))) + (y * sizeof(int)));

                if (a == b)
                    printf("%d\t", a);
            }
         }
      }
    } 
 }
void intersect(int**ptr1,int**ptr2,大小行,大小列)
{
int i=0,j=0,x=0,y=0;
对于(i=0;i
main
是什么?而且
int**
不是2D数组。奇怪的是,您没有收到关于
相交
声明冲突的警告,因为您有两个it@ChristianGibbons它是arrayRon的行和列,您永远不会声明它们col不允许作为参数。@RonAxm-不允许是什么意思?@RonAxm-在执行
ptr[x][y]
时,必须知道列数才能计算正确的地址。在这里,您不能使用
int**ptr
先生,这给了我一个错误,即下标[col]中不允许使用参数col.@RonAxm-你确定两个位置都修复了吗?看什么是2?我不想传递默认值,没有其他方法吗?@RonAxm我编辑了我的答案。你可以使用宏,因为宏在预处理器阶段被替换。