Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_Pointers - Fatal编程技术网

指向错误对象的C指针

指向错误对象的C指针,c,arrays,pointers,C,Arrays,Pointers,在我的代码中,我有一个包含10个分数对象的数组,出于测试目的,我只想编辑该数组中的第一个分数。我的.h文件如下: /*frac_heap.h*/ /*typedefs*/ typedef struct { signed char sign; unsigned int denominator; unsigned int numerator; }fraction; typedef struct { unsigned int isFree; }block; void

在我的代码中,我有一个包含10个分数对象的数组,出于测试目的,我只想编辑该数组中的第一个分数。我的.h文件如下:

/*frac_heap.h*/

/*typedefs*/

typedef struct
{
   signed char sign;
   unsigned int denominator;
   unsigned int numerator;
}fraction;

typedef struct
{
    unsigned int isFree;
}block;

void dump_heap();
void init_Heap();
fraction* new_frac(); 
在my.c文件中有以下内容:

// File frac_heap.c
#include <stdio.h>
#include <stdlib.h>
#include "frac_heap.h"

#define ARRAYSIZE 10

fraction* heap[ARRAYSIZE] = {};
block* freeBlocks[ARRAYSIZE] = {};
int startingBlock = 0;

void init_Heap(){
    int x;
    for(x = 0; x < ARRAYSIZE; x ++){    
        block *currBlock = &freeBlocks[x];
        currBlock->isFree = 1;  
    }

}
void dump_heap(){
    int x;
    for(x = 0; x < ARRAYSIZE; x ++){
        fraction* tempFrac = &heap[x];
        printf("%d\t%d\t%d\n",tempFrac->sign, tempFrac->numerator, tempFrac->denominator);
    }   

}

fraction* new_frac(){
    fraction* testFraction = &heap[0];
    return testFraction;
}  

int main(){

    init_Heap();

    fraction *p1;
    p1 = new_frac();
    p1->sign = -1;
    p1->numerator  = 2;
    p1->denominator = 3;
    dump_heap();
    return 0;
   }
-1  2   3
3   0   2
2   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
当我只有一个指向分数1的指针作为p1时,分数2和3是如何编辑的?我是否使用了错误的指针?

您有两个错误

第一个是,当从数组中获取指针时,使用操作符
&
的地址。当你在指针上使用它时,你得到的是指向指针的指针,而不是实际的指针

第二个问题是没有分配指针

因此,当您尝试取消引用结构指针时,不仅会访问错误的内存指针,而且如果删除
&
运算符,则会访问未分配的内存。这两种情况都会导致未定义的行为。第二个很可能会让你崩溃


也不能仅使用空大括号初始化数组。必须将要初始化数组的内容放在大括号内:

fraction* heap[ARRAYSIZE] = { NULL };
您需要对结构进行malloc(),或者定义固定大小的分数数组(如果大小是固定的)

备选方案#1:

备选方案2:

fraction*heap[ARRAYSIZE]={};
void init_Heap(){
int x;
对于(x=0;xisFree=1;
/*马洛克组分*/
heap[x]=(分数*)malloc(sizeof(分数));
堆[x]->分子=0;
堆[x]->分母=0;
堆[x]->符号=0;
}
}
void dump_heap(){
...
分数*tempFrac=heap[x];/*不能反引用heap*/
...
}
分数*new_frac(){
...
分数*测试分数=堆[0];
...
}
更改

fraction* heap[ARRAYSIZE] = {};
block* freeBlocks[ARRAYSIZE] = {};

类型*名称[大小]
:生成指针数组

你想要十个分数的物体。
freeBlocks[]
是一个由
block*
组成的数组,因此这个
block*currBlock=&freeblock[x]
应该没有
&
。这与
堆一起变成了
。将分数*heap[arraysize]改为={null};然后删除&before heap[0]和freeBlocks[x]的结果是分段错误。分段错误是因为数组的所有元素,
freeBlocks[]
heap[]
都没有适当地初始化或定义将分数*heap[arraysize]更改为={null};然后删除&before heap[0]与freeBlocks[x]一样,结果是分段错误。你到底在哪里分配指针?@user2250204这是因为你犯了第二个错误,没有为结构分配内存。例如,你的意思是,不要使用分数*testFraction=heap[0];在将其分配给堆[0]之前,我应该在自己的行中单独使用france*testfrance?我想你会提到这一点。在这个特定的问题上,malloc不可用,因为它是用来生成我们自己的内存块结构的。@user2250204那么数组应该是结构数组,而不是指向结构的指针数组。分数堆[ARRAYSIZE][10]={};如果ARRAYSIZE定义为50,那么这两个值不会冲突,因为它将是heap[50][10]?分数2-4在调用init_heap后立即显示1的分母。如果我注释掉init_heap,即使在列表中添加分数,它们都是正确的。block*currBlock指针为什么会影响heap中的项?
fraction* heap[ARRAYSIZE] = {};

void init_Heap(){
int x;
for(x = 0; x < ARRAYSIZE; x ++){    
    block *currBlock = &freeBlocks[x];
    currBlock->isFree = 1;  

    /*MALLOC FRACTIONS*/
    heap[x] = (fraction*)malloc(  sizeof(fraction));
    heap[x]->numerator=0;
    heap[x]->denominator=0;
    heap[x]->sign=0;
    }
}

void dump_heap(){
    ...
    fraction* tempFrac = heap[x]; /*You cannot de-reference heap*/
    ...
}

fraction* new_frac(){
    ...
    fraction* testFraction = heap[0];
    ...
}
fraction* heap[ARRAYSIZE] = {};
block* freeBlocks[ARRAYSIZE] = {};
fraction heap[ARRAYSIZE] = {0};
block freeBlocks[ARRAYSIZE] = {0};