gpt4 book ai didi

c 指向结构体的指针问题

转载 作者:行者123 更新时间:2023-11-30 21:10:17 25 4
gpt4 key购买 nike

有人可以帮我解决这个 c 指针问题吗?在我看来,它应该可以工作,但一定有一些我不理解的地方,因为代码无法编译。线路上写着:错误<<<<>>>>>>>>。我认为问题出在函数 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);
}

最佳答案

getDynArr 在使用之前未声明,声明位于文件的更下方。编译器应该对此发出警告。

由于 getDynArr 未声明,编译器不知道它采用什么参数或返回什么。然后,编译器会采用一些默认值,例如返回类型 int,这会导致您看到的错误。

要解决此问题,请在尝试使用函数之前声明该函数。

关于c 指向结构体的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30471965/

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