-1

How to combine byte arrays for accepts a variable number of arguments(variadic function) in c?

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

Thanks in advance

Zong
  • 6,160
  • 5
  • 32
  • 46
ant
  • 53
  • 1
  • 6
  • There is a lot more to this than just the variable number of arguments; I think that the main issue you have to think about is memory management. Handling various arguments is the easy part: look at [this question](http://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around) too. – Cyb3rFly3r Apr 27 '16 at 19:31
  • How to get arguments list from fmt in bytesConcat function? – ant Apr 27 '16 at 19:33
  • @anto By using `stdarg.h` library. Let us know when you encounter a specific problem using it. – 2501 Apr 27 '16 at 19:35
  • @2501, Thank you for point out. – ant Apr 29 '16 at 20:22

2 Answers2

0

@2501, Thank you for point out.

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();
}
ant
  • 53
  • 1
  • 6
-2

Use dynamic arrays / dynamic memory allocation: malloc() calloc() realloc() free()

At initialization allocate memory for the array using malloc() or calloc(). If the array gets bigger or smaller use realloc() to allocate the new amount of memory

An example is in this: 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);

See also https://www.happybearsoftware.com/implementing-a-dynamic-array

Because of the variadic function 2501's comment is correct. Use stdarg.h and see e.g. wikipedia for example (https://en.wikipedia.org/wiki/Variadic_function)

For concatenating arrays in C see How can I concatenate two arrays in C?

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • I am not the down voter here, but what does using dynamic memory allocation have to do with using veriadic functions? ( `bytes bytesConcat(bytes fmt, ...);` ) – ryyker Apr 27 '16 at 20:18
  • i think the resulting array is of variable length, therefore the dynamic memory allocation. Per argument the array grows and it is initially not determined how many arguments will be passed to the function... – ralf htp Apr 27 '16 at 20:40