C 程序语法(未编译)

标签 c pointers syntax

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

此 header 由教授提供,无法更改。

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 (错误在我的第一个函数的返回语句中)。有谁知道如何解决这一问题?很抱歉发布了一个非常简单的问题哈哈,我对 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) {

}

最佳答案

您的函数原型(prototype)说它返回带有 TYPE BStree 的东西,它是指向 float 的指针,但是您尝试返回一个 VARIABLE BStree 指向的整数。

所以你返回一个 int 而不是指向 float 的指针。

BStree bstree_ini(int size) {
    ...
    return *BStree;
}

您将类型和变量都称为 BStree。这非常令人困惑。先解决这个问题。

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

相关文章:

javascript - typescript 可以导出功能吗?

c++ - API 文档中的 void**

c - 猜单词 : I/O C logic error

c - ELF 文件生成命令和选项

c - 函数地址一致?

c - 在下面的代码中使用指针打印 2 个变量的数据

syntax - 为什么我不能在 Groovy 脚本中的 @Grab 声明之后进行方法调用?

c# - 不一致的语法 c#?

c - C 中的 GTK : Segmentation Fault when using key-release or key-press

c++ - 使 C++ 模拟类同时使用 2D 和 3D vector