Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 矩阵中节点的引用问题_C_Pointers_Reference_Null - Fatal编程技术网

C 矩阵中节点的引用问题

C 矩阵中节点的引用问题,c,pointers,reference,null,C,Pointers,Reference,Null,我将尝试解释我将在下一部分中放入的所有代码,首先我将告诉您问题所在,我正在丢失这个矩阵中一个指针的引用,该指针连接一个节点和它的邻居,我向您放置一个printf,以帮助您查看我丢失了什么样的引用,现在,我要做的程序是一个矩阵,其中包含的节点在他的atribute中有邻居上、下、左、右,我试图防止边缘情况在邻居属性中放置空指针,但我不知道为什么不能正常工作,因为空值突然变为1,我真的不知道为什么。 下面是代码:(这是我可以用来显示错误的较小代码) #包括 #包括 #包括 定义表结点 { int-i

我将尝试解释我将在下一部分中放入的所有代码,首先我将告诉您问题所在,我正在丢失这个矩阵中一个指针的引用,该指针连接一个节点和它的邻居,我向您放置一个printf,以帮助您查看我丢失了什么样的引用,现在,我要做的程序是一个矩阵,其中包含的节点在他的atribute中有邻居上、下、左、右,我试图防止边缘情况在邻居属性中放置空指针,但我不知道为什么不能正常工作,因为空值突然变为1,我真的不知道为什么。 下面是代码:(这是我可以用来显示错误的较小代码)

#包括
#包括
#包括
定义表结点
{
int-id;
int col;
int行;
int节点受限制;
结构节点*up;
结构节点*向下;
结构节点*左;
结构节点*右;
内色;
}节点;
Node init_Node(int-id、int-col、int-row){
节点新建_节点;
新节点id=id;
new_node.col=col;
new_node.row=行;
new_node.up=NULL;
new_node.down=NULL;
new_node.left=NULL;
new_node.right=NULL;
新建_node.color=0;
new_node.node_restricted=0;
返回新的_节点;
}
节点**与节点形成矩阵(节点的整数金额,轴的整数金额,节点的整数金额,轴的整数金额){
节点**矩阵,其中节点=calloc(节点的数量x轴,节点的大小*);
对于(int i=0;i<节点的数量\u轴\u x;i++)
{
矩阵_与_节点[i]=calloc(_节点的数量_轴的数量_y,大小(节点));
}
int total_of_node=_node_axis_x的金额_*_node_axis_y的金额_;
int行=0;
int col=0;
对于(int i=0;i<节点的总数;i++)
{
如果(((列+1)%u节点的数量\u轴\u x)==0)
{
矩阵_与_节点[row][col]=init_节点(i,row,col);
行=行+1;
col=0;
}否则{
矩阵_与_节点[row][col]=init_节点(i,row,col);
col=col+1;
}
}
_节点之间的链接(矩阵_与_节点、_节点的数量_、轴的数量_、轴的数量_);
矩阵_与_节点[0][0]。颜色=1;
矩阵_与_节点[0][2]。颜色=1;
矩阵_与_节点[0][3]。颜色=1;
printf(“这是我的左指针%d\n”,带有节点[1][0]的矩阵[u]。左);
矩阵_与_节点[0][4]。颜色=1;
printf(“这是我的左指针%d\n”,带有节点[1][0]的矩阵[u]。左);
带有节点[1][0]的矩阵[u]。颜色=1;
矩阵_与_节点[1][1]。颜色=1;
矩阵_与_节点[1][2]。颜色=1;
矩阵_与_节点[2][0]。颜色=1;
矩阵_与_节点[3][0]。颜色=2;
带_节点的返回矩阵_;
}
无效链接\u在\u节点之间(节点**矩阵\u与\u节点,节点的整数金额\u轴的整数金额\u x,节点的整数金额\u轴的整数金额\u y){
对于(int i=0;i<节点的数量\u轴\u x;i++)
{
对于(int j=0;j<节点的数量\u轴\u y;j++)
{            
//这是一个非常重要的问题
如果((i-1)>=0)
{            
矩阵_与_节点[i][j]。up=&(矩阵_与_节点[i-1][j]);
}//这是一个非常重要的问题
如果((i+1)=0)
{                
矩阵_与_节点[i][j]。左=&(矩阵_与_节点[i][j-1]);
}//这是一个非常重要的问题

if((j+1)一个问题是,如果要分配4个元素,则无法分配索引4处的元素。即,
matrix_与_节点[0][4].color=1;
至少需要5个元素。如果您的节点结构中有那么多指针,也许您不需要矩阵?wildplasser是的,我知道,但a需要矩阵来做指针:D Hanks EdmCoff这就是问题所在,我没有看到矩阵更小:D
#include <stdio.h>
#include <unistd.h>
#include <string.h>

typedef struct Node
{
    int id;  
    int col;
    int row;
    int node_restricted; 
    struct Node* up;
    struct Node* down;
    struct Node* left;
    struct Node* right;
    int color;

}Node;
Node init_node(int id,int col, int row){
    Node new_node;
    new_node.id=id;
    new_node.col = col;
    new_node.row = row;
    new_node.up = NULL;
    new_node.down = NULL;
    new_node.left = NULL;
    new_node.right = NULL;
    new_node.color = 0;
    new_node.node_restricted = 0;

    return new_node;
}
Node** form_matrix_with_node(int amount_of_node_axis_x,int amount_of_node_axis_y){

    Node** matrix_with_node = calloc(amount_of_node_axis_x,sizeof(Node*));

    for (int i = 0; i < amount_of_node_axis_x; i++)
    {
        matrix_with_node[i] = calloc(amount_of_node_axis_y,sizeof(Node));
    }
    
    
    int total_of_node = amount_of_node_axis_x*amount_of_node_axis_y;
    int row = 0;
    int col = 0;
    for (int i = 0; i < total_of_node; i++)
    {
        if (((col+1)%amount_of_node_axis_x)==0)
        {
            matrix_with_node[row][col] = init_node(i,row,col);
            row = row + 1; 
            col = 0;
        }else{
            matrix_with_node[row][col] = init_node(i,row,col);
            col = col + 1;    
        }
        
    }
    
    link_between_node(matrix_with_node,amount_of_node_axis_x,amount_of_node_axis_y);
    matrix_with_node[0][0].color = 1;
    matrix_with_node[0][2].color = 1;
    matrix_with_node[0][3].color = 1;
    printf("this is my left pointer  %d\n",matrix_with_node[1][0].left);
    matrix_with_node[0][4].color = 1;
    printf("this is my left pointer  %d\n",matrix_with_node[1][0].left);
    matrix_with_node[1][0].color = 1;
    matrix_with_node[1][1].color = 1;
    matrix_with_node[1][2].color = 1;
    matrix_with_node[2][0].color = 1;
    matrix_with_node[3][0].color = 2; 
    
    
    

    
    return matrix_with_node;
}

void link_between_node(Node** matrix_with_node,int amount_of_node_axis_x,int amount_of_node_axis_y){
    
    for (int i = 0; i < amount_of_node_axis_x; i++)
    {
        for (int j = 0; j < amount_of_node_axis_y; j++)
        {            
            //en el siguente se ve si la conexion con el nodo de arriba
            if ((i-1)>=0)
            {            
                matrix_with_node[i][j].up = &(matrix_with_node[i-1][j]);
            }//en el siguente se ve si la conexion con el nodo de abajo
            if ((i+1)<amount_of_node_axis_y)
            {                
                matrix_with_node[i][j].down = &(matrix_with_node[i+1][j]);
            }//en el siguente se ve si la conexion con el nodo de izquierda
            if ((j-1)>=0)
            {                
                matrix_with_node[i][j].left = &(matrix_with_node[i][j-1]);
            }//en el siguente se ve si la conexion con el nodo de derecha
            if ((j+1)<amount_of_node_axis_x)
            {
                matrix_with_node[i][j].right = &(matrix_with_node[i][j+1]);
            }
        }      
    }
}
int main(int argc, char** argv)
{
    form_matrix_with_node(4,4);
}