0

在我的代码中,我有一个包含 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(); 

在我的 .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;
   }

dump_heap() 的输出应该列出 10 个分数(它们的符号、分子和分母),其中分数 1 是唯一更改的分数。但是,输出如下:

-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?我使用指针错了吗?

4

3 回答 3

2

您需要 malloc() 您的结构或定义固定大小的分数数组(如果大小是固定的。

替代#1:

fraction heap[ARRAYSIZE][10] = {};

备选方案 #2:

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];
    ...
}
于 2013-04-06T11:50:09.963 回答
0

You have two errors...

The first is that you use the address-of operator & when you get the pointers from the array. When you use it on a pointer you get a pointer to a pointer, not the actual pointer.

The second problem is that you don't allocate the pointers.

So when you try to dererference the structure pointers not only will access wrong memory pointer, if you remove the & operator then you would access unallocated memory. Both of these will cause undefined behavior. The second of which will most likely give you a crash.


You also can't initialize an array with just empty braces. You have to put what you want to initialize the array to inside the braces:

fraction* heap[ARRAYSIZE] = { NULL };
于 2013-04-06T11:14:11.380 回答
0

change

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

to

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

type* name[SIZE] : make pointer array

you want ten fraction object. fraction arrayName[10];

于 2013-04-06T12:08:48.863 回答