Reputation: 1
//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;
}
This header is provided by the professor and cannot be changed.
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);
When I compile, it gives me this error: http://puu.sh/7y0Lp.png (the error is within the return statement for my first function). Does anyone know how to fix this? I apologize for posting a very simple question haha, I'm still quite new to C and pointers!
Here's the rest of the bst.c
that I haven't yet finished.
// 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) {
}
Upvotes: 0
Views: 136
Reputation: 4773
You say typedef float* BStree;
which means that BStree 'objects' are actually pointers.
The warning is due to the fact that you returned an int
pointer while the compiler expected a float
pointer (== BSTree)
BStree bstree_ini(int size) {
BStree tree;
tree = (BStree) malloc ((size+1)*sizeof(float)); /* Make size +1 places to store a float */
tree[0]=0.5 + (float)size; /* cast size to float */
int i;
for (i=1; i<=size; i++){ /*you need to start at 1 or you'll overwrite size+0.5*/
tree[i]=-1.0;
}
return tree; /* returning a pointer to a float* (or a BSTree)*/
}
Note that I did not dereference tree
; it is already a pointer
Upvotes: 1
Reputation: 12629
Your functions prototype says it returns something with TYPE BStree, which is pointer to a float, but you try to return an integer that VARIABLE BStree is pointing to.
So you return an int instead of a pointer to a float.
BStree bstree_ini(int size) {
...
return *BStree;
}
You call both a type and a variable BStree. That's extremely confusing. Fix that first.
Upvotes: 1
Reputation: 754730
The first visible definition is confused, if not confusing:
BStree bstree_ini(int size) {
int * BStree;
The first line says that bstree_ini()
is a function that takes an int
argument and returns a BStree
.
The second line says that BStree
is a local variable of type int *
.
You can't have both at once. Since you return *BStree
, the local variable is of the wrong type.
Since bstree_ini()
needs to return a pointer to an array of floats, you can write:
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;
}
I remain to be convinced by the design, but given that you can't change the header, this should do the trick for you.
This was provided as a possible answer before the confession that the header can't be changed.
The header defines:
typedef float *BStree;
which is a type I would not expect you to be using. You would normally be using a structure type:
typedef struct BStree
{
float value;
struct BStree *l_child;
struct BStree *r_child;
} BStree;
In your function, you'd then have:
BStree *bstree_ini(int size)
{
BStree *bp = …;
…
return bp;
}
Upvotes: 0