2016-10-22 5 views
0

Ich bin mehr oder weniger neu mit Zeigern in C, tut mir leid, wenn ich einige schreckliche Fehler mache! In diesem Fall versuche ich nur, alle Elemente eines Float-Vektors in einen anderen zu kopieren.Kann jemand erklären, warum ich diesen Fehler mit Typen bekomme?

Ich habe folgende Stück Code in meinem main.c-Datei, die gut funktioniert:

/* 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 ist ein Zeiger, der auf eine Struktur Punkte definiert in meine Linked-list.h Datei und _hash_list_ ist ein Vektor von Zeigern vom Typ Liste wo jeder zeigt auf eine Liste vom Typ Liste, in der gleichen Datei definiert:

/** 
* 
* 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; 

Dann zurück in meine main.c Datei, ich Schleife durch alle Zelle in meinem _hash_list_, vorbei an der Liste, die Zeiger der Zelle verweist, auf eine Funktion, die Daten aus der Liste aus und geben sie nicht, ich meine auf eine andere Funktion übernimmt:

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... */ 

} 

Die Funktion copyDelays in definiert ist mein Linked-list.c Datei:

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)); 
    } 
    } 
} 

Und schließlich, wenn ich versuche, alle meine Dateien zu kompilieren, erhalte ich diesen Fehler, Das verstehe ich nicht:

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 
+2

Was stimmt nicht mit 'delaysToCopy [i] = Verzögerungen [i];'? 'delayToCopy [i]' und 'delays [i]' sind Gleitkommazahlen, keine Zeiger. – tkausl

+0

Wenn ich dann einen ** _ free() _ ** oder ** _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ @ tkausl – wj127

+0

Ja, warum nicht? 'memcpy (dest, source, sizeof (float));' wobei 'dest' und' source 'float-pointer sind, ist _äquivalent_ zu' * dest = * source; '. – tkausl

Antwort

1

Das Problem ist, dass die Argumente, die Sie übergeben mEMCPY() Funktion nicht ma tch mit der Signatur der Funktion. memcpy() Funktion Unterschrift ist wie folgt: (http://www.cplusplus.com/reference/cstring/memcpy/)

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

Wie Sie es Zeiger als seine erste und das zweite Argument sehen nimmt, während delaysToCopy[i] und delays[i] sind keine Zeiger nur schwimmen.

Es sollte Ihr Problem lösen, wenn Sie Ampersand-Zeichen verwenden, um ihre Adressen zu erhalten, wie unten dargestellt:

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

Vielen Dank @J.Matt diese Erklärung hat mir das Problem verständlich gemacht, da es scheint, dass ich die Konzepte mit Zeigern in diesem Fall falsch verstanden habe ... – wj127

Verwandte Themen