2016-04-27 5 views
-1

Wie kombinieren Byte-Arrays für akzeptiert eine variable Anzahl von Argumenten (Variadic-Funktion) in c?Kombinieren Byte-Arrays für die Annahme einer variablen Anzahl von Argumenten

typedef struct { 
    unsigned char *data; 
    int length;   
} bytes; 

// Problem in here how to combine byte arrays 
// for accepts a variable number of arguments 
bytes bytesConcat(bytes fmt, ...) { 
    return b1 + b2 + ...b(n) 
} 

static unsigned char src1[] = { 1,2 }; 
static unsigned char src2[] = { 3,4 }; 

void main() { 
    bytes b1, b2; 

    b1.data = src1; 
    b1.length = sizeof(src1); 
    b2.data = src2; 
    b2.length = sizeof(src2); 

    // call byteConcat with passing two arguments 
    // result1 expected is 1,2,3,4 
    bytes result1 = bytesConcat(b1, b2); 
} 

Dank im Voraus

+1

Was haben Sie versucht? – 2501

+0

Es gibt viel mehr als nur die variable Anzahl von Argumenten; Ich denke, dass das Hauptproblem, über das Sie nachdenken müssen, das Speichermanagement ist. Der Umgang mit verschiedenen Argumenten ist der einfache Teil: Schauen Sie sich auch [diese Frage] an (http://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around). – Cyb3rFly3r

+0

Wie bekomme ich die Argumentliste von fmt in bytesConcat-Funktion? – ant

Antwort

0

@ 2501, Vielen Dank für Punkt aus.

unsigned char *appendBytes(int count, int *size, unsigned char *arg1, ...) { 
    int i, total; 
    unsigned char *tmp, *pd, *argn; 
    va_list ap; 

    if ((arg1 != NULL) && (count > 0)) { 
     total = 0; 
     for (i = 0; i < count; i++) { 
      total += size[i]; 
     } 

     if (total > 0) { 
      tmp = (unsigned char *)malloc((size_t)(total + 1)); 
      if (tmp != NULL) { 
       memset(tmp, null, sizeof(tmp)); 
       pd = tmp; 

       va_start(ap, arg1); 
       memcpy(pd, arg1, size[0]); 
       pd += size[0]; 
       for (i = 1; i < count; i++) { 
        argn = va_arg(ap, unsigned char*); 
        memcpy(pd, argn, size[i]); 
        pd += size[i]; 
       } 
       va_end(ap); 
       return tmp; 
      } 
     } 
    } 
    return NULL; 
} 

void main(){ 
    static unsigned char c1[] = { 1}; 
    static unsigned char c2[] = { 2, 3 }; 
    static unsigned char c3[] = { 4, 5, 6, 7, 8, 9, 10 }; 

    int size[] = { sizeof(c1), sizeof(c2), sizeof(c3) }; 
    unsigned char *result = appendBytes(3, size, c1, c2, c3); 

    // show output : 1 2 3 4 5 6 7 8 9 10 
    for (i = 0; i < sizeof(c1) + sizeof(c2)+ sizeof(c3); i++) { 
     printf("%u ", result[i]); 
    } 

    getchar(); 
} 
-2

Verwenden dynamische Arrays/dynamische Speicherzuweisung: malloc()calloc()realloc()free()

Bei der Initialisierung zugewiesen für die Array Speicher malloc() oder calloc() verwenden. Wenn das Array größer oder kleiner Einsatz bekommt realloc() die neue Menge an Speicher zuweisen

Ein Beispiel hierfür ist darin: Dynamic Array in C - Is my understanding of malloc/realloc correct?

double* data = (double*)malloc(10*sizeof(double)); 

for (j=0;j<10;j++) 
{` 
     data[j]= ((double)j)/100; 
     printf("%g, ",data[j]); 
} 

printf("\n"); 

data = (double*)realloc(data,11*sizeof(double)); 

for (j=0;j<11;j++) 
{ 
    if (j == 10){ data[j]= ((double)j)/100; } 
    printf("%g, ",data[j]); 
} 

free((void*) data); 

Siehe auch https://www.happybearsoftware.com/implementing-a-dynamic-array

Wegen der variadische Funktion 2501 Kommentar ist richtig. Verwenden Sie stdarg.h und sehen Sie z.B. wikipedia zum Beispiel (https://en.wikipedia.org/wiki/Variadic_function)

Für verketten Arrays in C siehe How can I concatenate two arrays in C?

+0

Ich bin hier nicht der Downvoter, aber was hat die Verwendung der dynamischen Speicherzuweisung mit der Verwendung von Veriadic-Funktionen zu tun? ('bytes bytesConcat (bytes fmt, ...);') – ryyker

+0

ich denke, das resultierende Array hat variable Länge, daher die dynamische Speicherzuweisung. Pro Argument wächst das Array und es wird zunächst nicht festgelegt, wie viele Argumente an die Funktion übergeben werden ... –

+0

Mein Problem ist gelöst, Danke. – ant