2017-09-24 1 views
-1

Mit einer Struktur dieses Typs kann eine Struktur in eine andere Struktur kopiert werden. Ich erklärte Array first und Array second, dann initiierte ich beide und legte verschiedene Daten in jedem.C - Kopieren von Strukturen mit Dynamikanordnungen

Dann zu first zu second kopieren Ich versuchte second = first, aber es funktioniert nicht.

Wie kann ich es tun?

#include <stdio.h> 
    #include <stdlib.h> 
    #include <inttypes.h> 


    ////////////////////////////////////// 

    typedef struct { 

     int group[8]; 
     uint64_t points; 

    } BestGroup; 

    ////////////////////////////////////// 

    typedef struct { 
     BestGroup *array; 
     size_t used; 
     size_t size; 
    } Array; 

    void initArray(Array *a, size_t initialSize) { 
     a->array = (BestGroup *)malloc(initialSize * sizeof(BestGroup)); 
     a->used = 0; 
     a->size = initialSize; 
    } 

    void insertArray(Array *a, int *group_add, uint64_t points_add) { 

     // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed. 
     // Therefore a->used can go up to a->size 
     if (a->used == a->size) 
     { 
     a->size *= 2; 
     a->array = (BestGroup *)realloc(a->array, a->size * sizeof(BestGroup)); 
     } 

     int i; 
     for (i = 0; i < 8; i++) 
     { 
     a->array[a->used].group[i] = group_add[i]; 
     } 
     a->array[a->used].points = points_add; 
     a->used++; 
    } 

    void freeArray(Array *a) { 
     free(a->array); 
     a->array = NULL; 
     a->used = a->size = 0; 
    } 


    void CopyArray(Array *a, Array *b) 
    { 
     b = a; 
    } 

    int main() 
    { 
     Array first; 
     Array second; 

     int first_data[8] = {0, 1, 2, 3, 4, 5, 6, 7}; 
     int second_data[8] = {7, 6, 5, 4, 3, 2, 1, 0}; 

     initArray(&first, 2); 
     initArray(&second, 2); 

     insertArray(&first, first_data, 5); 
     insertArray(&first, first_data, 5); 
     insertArray(&first, first_data, 5); 

     insertArray(&second, second_data, 2); 

     /////////////////////////////////////////// 

     printf("Total Points: %" PRIu64 "\n", first.array->points); 
     printf("Number: %lu\n\n", first.used); 
     printf("\n"); 

     int i; 
     int j; 

     for (i = 0; i < first.used; i++) 
     { 

      printf("["); 

      for (j = 0; j < 8; j++) 
      { 
      if (j) printf(", "); 
      printf("%d", first.array[i].group[j]); 
      } 

      printf("]\n"); 
     } 

     //////////////////////////////////////////// 

     printf("\n"); 
     printf("Total Points: %" PRIu64 "\n", second.array->points); 
     printf("Number: %lu\n\n", second.used); 
     printf("\n"); 

     for (i = 0; i < second.used; i++) 
     { 

      printf("["); 

      for (j = 0; j < 8; j++) 
      { 
      if (j) printf(", "); 
      printf("%d", second.array[i].group[j]); 
      } 

      printf("]\n"); 
     } 

     ///////////////////////////////// 

     CopyArray(&first, &second); 


     printf("\n"); 
     printf("\n"); 
     printf("\n"); 
     printf("\n"); 

     /////////////////////////////////////////// 

     printf("Total Points: %" PRIu64 "\n", first.array->points); 
     printf("Number: %lu\n\n", first.used); 
     printf("\n"); 

     for (i = 0; i < first.used; i++) 
     { 

      printf("["); 

      for (j = 0; j < 8; j++) 
      { 
      if (j) printf(", "); 
      printf("%d", first.array[i].group[j]); 
      } 

      printf("]\n"); 
     } 

     //////////////////////////////////////////// 

     printf("\n"); 
     printf("Total Points: %" PRIu64 "\n", second.array->points); 
     printf("Number: %lu\n\n", second.used); 
     printf("\n"); 

     for (i = 0; i < second.used; i++) 
     { 

      printf("["); 

      for (j = 0; j < 8; j++) 
      { 
      if (j) printf(", "); 
      printf("%d", second.array[i].group[j]); 
      } 

      printf("]\n"); 
     } 

     return 0; 
    } 

Die Ausgabe lautet:

Total Points: 5 
Number: 3 


[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

Total Points: 2 
Number: 1 


[7, 6, 5, 4, 3, 2, 1, 0] 




Total Points: 5 
Number: 3 


[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

Total Points: 2 
Number: 1 


[7, 6, 5, 4, 3, 2, 1, 0] 

Wenn es mit der letzten Liste als enden sollte:

Total Points: 5 
Number: 3 


[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

Total Points: 5 
Number: 3 


[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

EDIT

Wie vorgeschlagen habe ich memcpy(), Also habe ich die CopyArray() Funktion geändert in:

void CopyArray(Array *a, Array *b) 
    { 
     // b = a; 
     memcpy(b, a, a->size * sizeof(BestGroup)); 
    } 

Es sieht aus wie das Ergebnis in Ordnung sein wird, bis sie das Ende des Programms und Ausgänge

*** stack smashing detected ***

+0

Wo die entsprechende Zeile in dem obigen Code ist? Es sollte funktionieren, wenn sie denselben Typ haben (Sie können memcpy() wie bereits vorgeschlagen ausprobieren). –

+1

Der Großteil des von Ihnen angegebenen Codes scheint für die Frage irrelevant zu sein. Wir mögen Code sehen, aber generell wollen wir einen [mcve], mit Schwerpunkt in diesem Fall auf "* minimal *". Aber vernachlässigen Sie nicht "vollständig". Es ist nicht klar, was "es nicht funktioniert" bedeutet hier, oder wie Sie das bestimmen. Insbesondere können Sie den Operator '=' verwenden, um den Wert einer ganzen Struktur in eine andere Struktur desselben Typs zu kopieren. –

+0

Nur die Hauptfunktion der Frage sowie die tatsächlichen und die erwarteten Ausgaben hinzugefügt – PyCV

Antwort

1
erreicht

Dieser Code erzeugt die Antwort, die Sie wollen. Die CopyArray()-Funktion gibt jetzt die Daten bereits in b frei, initialisiert dann b und kopiert schließlich die Daten von a zu b. Es gibt Orte, wo der Code 8 verwendet, aber sollte wahrscheinlich eine andere Variable für die Größe verwenden - ich habe sie in diesem Code kommentiert. Die Funktion ist ein Beispiel für eine Funktion, die ich routinemäßig schreibe und beim Debuggen einer komplexen Struktur verwendet. Es dauert eine Tag-Zeichenfolge (mit der Sie identifizieren können, welcher Anruf gerade gedruckt wird) und das zu druckende Objekt. Ich nehme oft auch ein File-Stream-Argument und füge im Zweifelsfall einen fflush() für den Ausgabestrom hinzu.

/* SO 4639-4467 */ 
#include <inttypes.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct 
{ 
    int group[8]; 
    uint64_t points; 
} BestGroup; 

typedef struct 
{ 
    BestGroup *array; 
    size_t used; 
    size_t size; 
} Array; 

static void initArray(Array *a, size_t initialSize) 
{ 
    a->array = (BestGroup *)malloc(initialSize * sizeof(BestGroup)); 
    a->used = 0; 
    a->size = initialSize; 
} 

static void insertArray(Array *a, const int *group_add, uint64_t points_add) 
{ 
    if (a->used == a->size) 
    { 
     a->size *= 2; 
     a->array = (BestGroup *)realloc(a->array, a->size * sizeof(BestGroup)); 
    } 

    for (int i = 0; i < 8; i++)  // Why 8 and not points_add? 
    { 
     a->array[a->used].group[i] = group_add[i]; 
    } 
    a->array[a->used].points = points_add; 
    a->used++; 
} 

static void freeArray(Array *a) 
{ 
    free(a->array); 
    a->array = NULL; 
    a->used = a->size = 0; 
} 

static void CopyArray(const Array *a, Array *b) 
{ 
    freeArray(b); 
    initArray(b, a->used); 
    memmove(b->array, a->array, a->used * sizeof(a->array[0])); 
    b->used = a->used; 
} 

static void dump_array(const char *tag, const Array *arr) 
{ 
    printf("Array: %s\n", tag); 
    printf("Total Points: %" PRIu64 "\n", arr->array->points); 
    printf("Number: %lu\n", arr->used); 

    for (size_t i = 0; i < arr->used; i++) 
    { 
     printf("["); 
     for (size_t j = 0; j < 8; j++) // Why 8 and not arr->array[i].points? 
     { 
      if (j) 
       printf(", "); 
      printf("%d", arr->array[i].group[j]); 
     } 
     printf("]\n"); 
    } 
    putchar('\n'); 
} 

int main(void) 
{ 
    Array first; 
    Array second; 

    int first_data[8] = {0, 1, 2, 3, 4, 5, 6, 7}; 
    int second_data[8] = {7, 6, 5, 4, 3, 2, 1, 0}; 

    initArray(&first, 2); 
    initArray(&second, 2); 

    insertArray(&first, first_data, 5); 
    insertArray(&first, first_data, 5); 
    insertArray(&first, first_data, 5); 
    insertArray(&second, second_data, 2); 

    dump_array("first", &first); 
    dump_array("second", &second); 

    CopyArray(&first, &second); 

    printf("\n"); 
    dump_array("first", &first); 
    dump_array("second", &second); 

    return 0; 
} 

Der Ausgang dieser vom Laufen ist:

Array: first 
Total Points: 5 
Number: 3 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

Array: second 
Total Points: 2 
Number: 1 
[7, 6, 5, 4, 3, 2, 1, 0] 


Array: first 
Total Points: 5 
Number: 3 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 

Array: second 
Total Points: 5 
Number: 3 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7] 
[0, 1, 2, 3, 4, 5, 6, 7]