C程序语法(不编译)

用户名

bst.c

//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;
}

bst.h

该标题由教授提供,不能更改。

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) {

}
乔纳森·勒夫勒(Jonathan Leffler)

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

BStree bstree_ini(int size) {
     int * BStree;

第一行说这bstree_ini()是一个接受int参数并返回的函数BStree

第二行说这BStree是type的局部变量int *

您不能一次拥有两个。由于您返回*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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章