Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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 - Fatal编程技术网

C 指向链接列表中的结构

C 指向链接列表中的结构,c,pointers,C,Pointers,我们试图将结构的地址设置为给定的地址,但当我们打印出结构的地址时,它的值似乎与给定的地址不同 /*a struct to keep block information*/ struct header{ int space; int free; /* 1 = free space and 0 = full*/ struct header *nextHead; struct header *prevHead; }; typedef struct header nod

我们试图将结构的地址设置为给定的地址,但当我们打印出结构的地址时,它的值似乎与给定的地址不同

/*a struct to keep block information*/
struct header{
    int space;
    int free; /* 1 = free space and 0 = full*/
    struct header *nextHead;
    struct header *prevHead;
};

typedef struct header node;

int myinit(int *array, int size){

    int newSize = size;
    node * nullPointer;
    nullPointer = NULL; //make intermediatry node pointer for some bullshit reason

    * (array) = newSize;  /*store the size of the malloc at the first address*/

    printf("Address : %p\n", &array[0]);
    array++;
    printf("Address  after: %p\n", &array[0]);

    /*initial block*/
    node *root = (node *)&array; /*store the root at the next address available*/
    printf("size of struct %lu\n", sizeof(struct header));

    printf("%p\n", root);

    root->space = newSize;
    root->free = 1;
    root->nextHead = nullPointer;
    root->prevHead = nullPointer;


}
排队

node *root = (node *)&array;
您正在获取“array”局部变量的地址。瞧,你取的是堆栈上的值的地址,而不是你所期望的。您必须修改函数的签名,如下所示:

int mymain(int **myarray, int size);
node->nextHeader = NULL;
并相应修改其定义。然后,你可以写:

node *root = (node *)array;
排队

node *root = (node *)&array;
您正在获取“array”局部变量的地址。瞧,你取的是堆栈上的值的地址,而不是你所期望的。您必须修改函数的签名,如下所示:

int mymain(int **myarray, int size);
node->nextHeader = NULL;
并相应修改其定义。然后,你可以写:

node *root = (node *)array;

另外,在这段代码中使用
array
,而不是使用
&array[0]


如果你坚持使用简单的代码并理解你写的每一行代码,你就会少被指针弄糊涂。当你在一行中有大量的符号和特殊符号时,你可能做错了什么,训练你的蜘蛛意识以应对这些情况。

此外,在这段代码中使用
数组而不是
&array[0]

node *root = (node *)&array; 
如果你坚持使用简单的代码并理解你写的每一行代码,你就会少被指针弄糊涂。当你在一行中有很多符号和特殊符号时,你可能做错了什么,训练你的蜘蛛意识来应对这些情况

node *root = (node *)&array; 
在这里,您可以获取一个指针的地址,并将其强制转换为另一个指针。你不应该这样做。在此,您必须为节点分配内存:

 node * root = (node *) malloc(sizeof(node));
// or  this allocates the memory and puts zeros to it     
node * root = (node *) calloc(1, sizeof(node)); 
此外,您不需要任何指向NULL的节点,您可以像这样简单地使用NULL:

int mymain(int **myarray, int size);
node->nextHeader = NULL;
在这里,您可以获取一个指针的地址,并将其强制转换为另一个指针。你不应该这样做。在此,您必须为节点分配内存:

 node * root = (node *) malloc(sizeof(node));
// or  this allocates the memory and puts zeros to it     
node * root = (node *) calloc(1, sizeof(node)); 
此外,您不需要任何指向NULL的节点,您可以像这样简单地使用NULL:

int mymain(int **myarray, int size);
node->nextHeader = NULL;

代码乍一看很好,问题出在哪里?代码乍一看很好,问题出在哪里?答案很好,但我要补充的是,函数定义应该使用节点**,而不是int**。另外,我不知道为什么有人会首先将链表节点存储在数组中,或者为什么
newSize
存储在两个不同的位置。这里可能没有正确理解接口要求。答案很好,但我要补充的是,函数定义应该使用
节点**
,而不是
int**
。另外,我不知道为什么有人会首先将链表节点存储在数组中,或者为什么
newSize
存储在两个不同的位置。这里可能没有正确理解接口要求。在我的代码中,我正在重写malloc,因此我无法使用任何对malloc的隐式调用,您是否可以看到创建结构并能够在其中设置值的另一种方法。通过直接将值设置为NULL,我们无法编译,我们必须创建一个中间值来将其设置为NULL。会出现什么编译错误?用另一种分配内存的方法更新了答案在我的代码中,我正在重写malloc,因此我不能使用任何对malloc的隐式调用,您能看到另一种创建结构并能够在其中设置值的方法吗。通过直接将值设置为NULL,我们无法编译,我们必须创建一个中间值来将其设置为NULL。会出现什么编译错误?用另一种分配内存的方法更新了答案