英国夏令时

//my bst.c
#include <stdio.h>
#include <stdlib.h>
#include "bst.h"
// Input: 뭩ize? size of an array
// Output: a pointer to an allocated array
// Effect: dynamically allocate an array of size+1 float-point numbers
//         the first slot of the array contains size+0.5
//         all the other slots contain -1;
BStree bstree_ini(int size) {
     int * BStree;
     BStree = (int *) malloc ((size+1)*sizeof(float)); //?
     BStree[0]=size+0.5; //?????
     int i;
     for (i=0; i<size; i++){
         BStree[i]=-1;
     }
     return *BStree;
}

英国夏令时
此标题由教授提供,不能更改。
typedef float* BStree;
const float my_epsilon = 0.0001;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, float key);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst);

问题
当我编译时,它会给我这个错误:http://puu.sh/7y0Lp.png(错误在第一个函数的return语句中)。有人知道怎么解决这个问题吗?我很抱歉发了一个很简单的问题哈哈,我对C和指针还是很陌生的!
其余代码
以下是我尚未完成的bst.c部分。
// Input: 뭕st? a binary search tree
// 뭟ey? a non-negative floating-point number
// Effect: key is inserted into bst if it is not already in bst
void bstree_insert(BStree bst, float key) {

}
// Input: 뭕st? a binary search tree
// Effect: print all the numbers in bst using in order traversal
void bstree_traversal(BStree bst) {

}
// Input: 뭕st? a binary search tree
// Effect: memory pointed to by bst is freed
void bstree_free(BStree bst) {

}

最佳答案

第一个可见的定义是混乱的,如果不混乱的话:

BStree bstree_ini(int size) {
     int * BStree;

第一行表示bstree_ini()是一个接受int参数并返回BStree的函数。
第二行表示BStreeint *类型的局部变量。
你不能同时拥有两个。因为返回*BStree,所以局部变量的类型错误。
第二次尝试
由于bstree_ini()需要返回指向浮点数组的指针,因此可以编写:
BStree bstree_ini(int size)
{
     BStree;
     BStree data = malloc ((size + 1) * sizeof(*data));
     data[0] = size + 0.5;
     for (int i = 1; i <= size; i++)
         data[i] = -1.0;
     return data;
}

我仍然相信的设计,但鉴于你不能改变标题,这应该为你做的伎俩。
第一次尝试
这是在供认头像不能更改之前提供的一个可能的答案。
标题定义:
typedef float *BStree;

这是我不希望你使用的类型。您通常会使用结构类型:
typedef struct BStree
{
    float      value;
    struct BStree *l_child;
    struct BStree *r_child;
} BStree;

在你的职责中,你会有:
BStree *bstree_ini(int size)
{
    BStree *bp = …;
    …
    return bp;
}

关于c - C程序语法(不编译),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22443075/

10-12 20:56