【问题标题】:Can someone explain why I get this error with types?有人可以解释为什么我会收到此类型错误吗?
【发布时间】:2016-10-22 11:15:11
【问题描述】:

我在 C 语言中使用指针或多或少是新手,如果我犯了一些可怕的错误,非常抱歉!在这种情况下,我只是想将一个浮点向量的所有元素复制到另一个向量中。

我的 main.c 文件中有以下代码块,可以正常工作:

/* NOTE: hash_list is a global variable*/
void insertDataIntoOurHashList(int dia, int delay, char *aeO, char *aeD){
    unsigned int numHash;
    ListData *listData;

    numHash = getHashValue(aeO);

    /* If there's no list yet in this position, then... */
    if (hash_list[numHash] == NULL) {
        hash_list[numHash] = malloc(sizeof(List));
        initList(hash_list[numHash]);
        listData = malloc(sizeof(ListData));
        listData->key = malloc(sizeof(char*)*strlen(aeD)+1);
        strcpy(listData->key, aeD);
        listData->key_sec = malloc(sizeof(char*)*strlen(aeO)+1);
        strcpy(listData->key_sec, aeO);
        listData->numTimes = 1;
        listData->retrasos = (float*) malloc(sizeof(float)*7);
        listData->retrasos[dia-1] = delay;
        insertList(hash_list[numHash], listData);
    }
    else {
        listData = findList2(hash_list[numHash],aeD,aeO);

        /* If already exists a data with both equals keys, then... */
        if (listData != NULL) {
            listData->numTimes++; // We add in one the counter of the list
            listData->retrasos[dia-1] = listData->retrasos[dia-1] + delay / 2;
        }
        /* If exists a data with the same aeD as primary key but not with the aeO as secundary key, then... */
        else {
            listData = malloc(sizeof(ListData));
            listData->key = malloc(sizeof(char*)*strlen(aeD)+1);
            strcpy(listData->key, aeD);
            listData->key_sec = malloc(sizeof(char*)*strlen(aeO)+1);
            strcpy(listData->key_sec, aeO);
            listData->numTimes = 1;
            listData->retrasos = (float*) malloc(sizeof(float)*7);
            listData->retrasos[dia-1] = delay;
            insertList(hash_list[numHash], listData);
        }
    }
    free(aeO);
    free(aeD);
}

ListData *listData 是指向我的 linked-list.h 中定义的结构的指针文件,而 _hash_list_List 类型的指针向量,其中每个指针指向 List,在同一个文件中定义:

/**
*
* The TYPE_LIST_KEY is used to define the type of the primary
* key used to index data in the list.
*
*/

#define TYPE_LIST_KEY char*

/**
 *
 * This structure holds the information to be stored at each list item.  Change
 * this structure according to your needs.  In order to make this library work,
 * you also need to adapt the functions compEQ and freeListData. For the
 * current implementation the "key" member is used search within the list.
 *
 */

typedef struct ListData_ {
  // The variable used to index the list has to be called "key".
  TYPE_LIST_KEY key;
  char *key_sec;
  // This is the additional information that will be stored
  // within the structure. This additional information is associated
  // to the key. You may include any field you may need useful.
  float *retrasos;
  int numTimes;
} ListData;

/**
*
* The item structure
*
*/

typedef struct ListItem_ {
  ListData *data;
  struct ListItem_ *next;
} ListItem;

/**
*
* The list structure
*
*/

typedef struct List_ {
  int numItems;
  ListItem *first;
} List;

然后,回到我的 main.c 文件中,我循环遍历我的 _hash_list_ 中的每个单元格,将列表传递给那个指针单元格指向一个函数,该函数从列表中获取数据并将它们传递给另一个函数,我的意思是:

void insertInHash(){
  int i;

  for(i = 0; i < HASH_SIZE; i++){
    if (hash_list[i] != NULL) {
      dumpList2(hash_list[i]);
    }

  }
}

/* This function is called for every cell while looping the hash_list */
void dumpList2(List *l){
  ListItem *current;

  current = l->first;

  while (current != NULL)
  {
    insertDataIntoOurTree(current->data->key_sec, current->data->key, current->data->retrasos);
    current = current->next;
  }

}


void insertDataIntoOurTree(char *aeO, char *aeD, float *delays){
  List *list;
  ListData *listData;
  int i;

  /* Case when the node exists! */
  if (treeData != NULL) {
    treeData->num++; // We add in one the counter of treeData
    listData = findList(treeData->list, aeD); // We check if the new destination airport is inside the list of the node...

    /* If the destination is inside the list... */
    if(listData != NULL)
      listData->numTimes++; // We add in one the counter of the list

    /* If the destination isn't inside... */
    else {
      /* We create and initialize the new item the list of the node will contain! */
      listData = malloc(sizeof(ListData));
      listData->key = malloc(sizeof(char*)*strlen(aeD)+1); // Counting with the final '\0' byte!
      strcpy(listData->key, aeD); // Remember the case as above with aeO and aeD
      listData->numTimes = 1;
      listData->retrasos = (float*) malloc(sizeof(float)*7);
      //listData->retrasos[dia-1] = delay; // minus one cos we don't want to be out of bound! ;)
      //copyDelays(listData->retrasos, delays);
      for (i = 0; i < 7; i++) {
        listData->retrasos[i] = 0.0;
      }
      copyDelays(listData->retrasos, delays);

      insertList(treeData->list, listData);
    }
  }
  /* THERE ARE MORE CASES DOWN HERE, BUT THEY DON'T MATTER NOW... */

}

copyDelays 函数在我的 linked-list.c 文件中定义:

void copyDelays(float *delaysToCopy, float *delays){
  int i;

  for (i = 0; i < 7; i++) {
    if (delaysToCopy[i] == 0.0) {
      memcpy(delaysToCopy[i], delays[i], sizeof(float));
    }
  }
}

最后,当我尝试编译所有文件时,我收到了这个我不明白的错误:

linked-list.c:234:14: error: passing 'float' to parameter of incompatible type 'const void *'
      memcpy(delaysToCopy[i], delays[i], sizeof(float*));
         ^~~~~~~~~~~~~~~
/usr/include/secure/_string.h:65:59: note: expanded from macro 'memcpy'
  __builtin___memcpy_chk (dest, src, len, __darwin_obsz0 (dest))
                                                      ^~~~
/usr/include/secure/_common.h:38:55: note: expanded from macro '__darwin_obsz0'
#define __darwin_obsz0(object) __builtin_object_size (object, 0)
                                                  ^~~~~~
1 error generated.
make: *** [linked-list.o] Error 1

【问题讨论】:

  • delaysToCopy[i] = delays[i]; 有什么问题? delaysToCopy[i]delays[i] 是浮点数,而不是指针。
  • 那么如果我做了一个 free()delete 的 'delays' 向量,我的 'delaysToCopy' 里面还会有值吗? @tkausl
  • 是的,为什么不呢? memcpy(dest, source, sizeof(float)); 其中destsource 都是浮点指针等效于*dest = *source;
  • 你是对的@tkausl,我误解了概念并使自己一团糟,试图做类似下面的答案......无论如何,谢谢!现在我对这个概念理解得更清楚了!

标签: c pointers data-structures memcpy


【解决方案1】:

问题是您传递给 memcpy() 函数的参数与函数的签名不匹配。 memcpy() 函数签名如下:(http://www.cplusplus.com/reference/cstring/memcpy/)

void * memcpy ( void * destination, const void * source, size_t num );

如您所见,它将指针作为其第一个和第二个参数,而 delaysToCopy[i]delays[i] 不是指针,只是浮动。

如果你使用&符号来获取他们的地址应该可以解决你的问题,如下所示:

memcpy(&delaysToCopy[i], &delays[i], sizeof(float));

【讨论】:

  • 非常感谢@J。马特这个解释让我理解了这个问题,因为在这种情况下我似乎误解了带有指针的概念......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 2017-03-17
  • 1970-01-01
相关资源
最近更新 更多