gpt4 book ai didi

C 程序语法(未编译)

转载 作者:太空宇宙 更新时间:2023-11-04 08:43:53 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com