-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

4
  • 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 too. Commented Apr 27, 2016 at 19:31
  • How to get arguments list from fmt in bytesConcat function? Commented Apr 27, 2016 at 19:33
  • @anto By using stdarg.h library. Let us know when you encounter a specific problem using it. Commented Apr 27, 2016 at 19:35
  • @2501, Thank you for point out. Commented Apr 29, 2016 at 20:22

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

-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?

2 Comments

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, ...); )
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.