请有人帮我解决这个问题。在我看来它应该工作,但必须有一些我不理解的东西,因为代码没有编译。在说明这一点的行上:ERROR<<<<>>>>>>>>>。我认为问题在于函数getDynArr位于标签处:GETDYNARR<<<<<<<<<<<<<<<<<<<<< <<>>>>>>>>>>>>>>>取代。我认为getDynArr(da,i)可能因某种原因返回int?
/* dynArr.c: Dynamic Array implementation. */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef KEYTYPE
#define KEYTYPE char *
#endif
#ifndef VALUETYPE
#define VALUETYPE double
#endif
#ifndef TYPE
#define TYPE struct association
//# define TYPE int
#endif
struct association
{
KEYTYPE key;
VALUETYPE value;
};
struct DynArr
{
TYPE *data; /* pointer to the data array */
// struct association *data;
int size; /* Number of elements in the array */
int capacity; /* capacity ofthe array */
};
///////////////////dictionary Section
void dyArrayDictionaryPut(struct DynArr *vec, KEYTYPE key, VALUETYPE val)
{
struct association *ap;
if (dyArrayDictionaryContainsKey(vec,key))
dyArrayDictionaryRemove(vec, key);
ap = malloc(sizeof(struct association));
assert(ap != 0);
ap->key = key;
ap->value = val;
//dyArrayAdd(vec, ap);
addDynArr(vec, ap);
}
VALUETYPE dyArrayDictionaryGet(struct DynArr *da, KEYTYPE key, VALUETYPE *valptr)
{
struct association *ap;
int i = 0;
if(dyArrayDictionaryContainsKey(da, key))
{
for ( i = 0; i < da->size; i++)
{
//if(da->data[i]->key == key)
if(da->data[i].key == key)
{
//return da->data->value;
struct association temp3;
temp3 = getDynArr(da, i); //ERROR<<<<<ERROR>>>>>>>>>incompatible types when assigning to type 'struct association' from type 'int'
//ap = getDynArr(da, i);
//*valptr = ap->value;
}
}
}
}
int dyArrayDictionaryContainsKey(struct DynArr *da, KEYTYPE key)
{
int i = 0;
for (i = 0; i < da->size; i++)
{
if(da->data[i].key == key)
{
return 1;
}
}
return 0;
}
void dyArrayDictionaryRemove(struct DynArr *da, KEYTYPE key)
{
for (int i = 0; i < da->size; i++)
{
if(da->data[i].key == key)
{
removeAtDynArr(da, i);
return;
}
}
}
////
void _initDynArr(struct DynArr *v, int capacity)
{
assert(capacity > 0);
assert(v!= 0);
v->data = malloc(sizeof(TYPE) * capacity);
assert(v->data != 0);
v->size = 0;
v->capacity = capacity;
}
struct DynArr* createDynArr(int cap)
{
struct DynArr *r;
assert(cap > 0);
r = malloc(sizeof( struct DynArr));
assert(r != 0);
_initDynArr(r,cap);
return r;
}
void freeDynArr(struct DynArr *v)
{
assert(v!=0);
if(v->data != 0)
{
free(v->data); /* free the space on the heap */
v->data = 0; /* make it point to null */
}
v->size = 0;
v->capacity = 0;
}
void deleteDynArr(struct DynArr *v)
{
assert (v!= 0);
freeDynArr(v);
free(v);
}
void _dynArrSetCapacity(struct DynArr *v, int newCap)
{
int i;
TYPE *oldData;
int oldSize = v->size;
oldData = v->data;
printf("========Resizing========\n");
/* Create a new dyn array with larger underlying array */
_initDynArr(v, newCap);
for(i = 0; i < oldSize; i++){
v->data[i] = oldData[i];
}
v->size = oldSize;
/* Remember, init did not free the original data */
free(oldData);
#ifdef ALTERNATIVE
int i;
/* Create a new underlying array*/
TYPE *newData = (TYPE*)malloc(sizeof(TYPE)*newCap);
assert(newData != 0);
/* copy elements to it */
for(i = 0; i < v->size; i++)
{
newData[i] = v->data[i];
}
/* Delete the oldunderlying array*/
free(v->data);
/* update capacity and size and data*/
v->data = newData;
v->capacity = newCap;
#endif
}
int sizeDynArr(struct DynArr *v)
{
assert(v!=0);
return v->size;
}
void addDynArr(struct DynArr *v, TYPE val)
{
assert(v!=0);
/* Check to see if a resize is necessary */
if(v->size >= v->capacity)
_dynArrSetCapacity(v, 2 * v->capacity);
v->data[v->size] = val;
v->size++;
}
//GETDYNARR<<<<<<<<<<<<<<<<<<<<<GETDYNARR>>>>>>>>>>>>>>>>>
//type is struct association
//TYPE getDynArr(struct DynArr *v, int pos )
struct association getDynArr(struct DynArr *v, int pos)
{
assert(v!=0);
assert(pos < v->size);
assert(pos >= 0);
struct association returned = v->data[pos];
return returned;
}
void putDynArr(struct DynArr *v, int pos, TYPE val)
{
assert(v!=0);
assert(pos < v->size);
assert(pos >= 0);
v->data[pos] = val;
}
void swapDynArr(struct DynArr *v, int i, int j)
{
TYPE temp;
assert(v!=0);
assert(i < v->size);
assert(j < v->size);
assert(i >= 0);
assert(j >= 0);
temp = v->data[i];
v->data[i] = v->data[j];
v->data[j] = temp;
}
void removeAtDynArr(struct DynArr *v, int idx){
int i;
assert(v!= 0);
assert(idx < v->size);
assert(idx >= 0);
//Move all elements up
for(i = idx; i < v->size-1; i++){
v->data[i] = v->data[i+1];
}
v->size--;
}
int main()
{
// struct DynArr *dictionary = createDynArr(2);
// dyArrayDictionaryPut(dictionary, "key1", 2);
//double result = dyArrayDictionaryGet(dictionary, "key1");
//dyArrayDictionaryContainsKey(struct DynArr *da, KEYTYPE key)
//dyArrayDictionaryRemove(struct DynArr *da, KEYTYPE key)
//printf("%f\n",result);
}
答案 0 :(得分:0)
getDynArr
在使用之前未声明,声明在文件中进一步向下。编译器应该警告你。
由于getDynArr
未声明,编译器不知道它需要什么参数或返回什么。然后,编译器会假定某些默认值,例如返回类型int
,这会导致您看到的错误。
要解决此问题,请在尝试使用之前声明该函数。
答案 1 :(得分:0)
我不打算讨论实施,因为你没有要求它。 为了编译,您需要修复以下内容:
缺少函数声明:getDynArr,dyArrayDictionaryRemove,dyArrayDictionaryContainsKey,addDynArr,removeAtDynArr;因此,您需要在struct声明的正下方添加函数签名,例如:
struct association
{
KEYTYPE key;
VALUETYPE value;
};
struct DynArr
{
TYPE *data; /* pointer to the data array */
// struct association *data;
int size; /* Number of elements in the array */
int capacity; /* capacity ofthe array */
};
///////////////////dictionary Section
struct association getDynArr(struct DynArr *v, int pos);
void dyArrayDictionaryRemove(struct DynArr *da, KEYTYPE key);
int dyArrayDictionaryContainsKey(struct DynArr *da, KEYTYPE key);
void addDynArr(struct DynArr *v, TYPE val);
void removeAtDynArr(struct DynArr *v, int idx);
...
在函数dyArrayDictionaryPut中你需要调用addDynArr根据函数签名按值传递struct,所以第54行的调用将是:
addDynArr(vec, *ap);
您需要将其编译为c99代码,否则您需要删除每个for循环的变量初始化(例如gcc选项为-std = c99);
然后让我向您提供一些您可能会觉得有用的建议:
您可能需要考虑使用typedef而不是define来为struct关联创建别名;
typedef struct
{
KEYTYPE key;
VALUETYPE value;
} association;
然后在这一点上你可以使用“关联”作为一种类型而不需要提及它是一个结构(我猜你也可以为它找到一个更好的名字);