2016-04-27 4 views
-1

バイト配列を組み合わせて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); 
} 

おかげ

+1

何を試しましたか? – 2501

+0

引数の可変数だけではなく、これ以上のことがあります。私はあなたが考える必要がある主な問題はメモリ管理だと思います。さまざまな議論を扱うことは簡単な部分です:[この質問](http://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around)もご覧ください。 – Cyb3rFly3r

+0

fmtの引数リストをbytesConcat関数で取得する方法は? – ant

答えて

0

参照してください。

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

使用ダイナミックアレイ/動的メモリ割り当て:初期化時malloc()calloc()realloc()free()

malloc()又はcalloc()を使用してアレイにメモリを割り当てます。配列はメモリ

例の新しい量を割り当てるように大きくなったり小さく使用realloc()を取得した場合、この中にある。また、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); 

見るので可変引数機能 2501のコメントのhttps://www.happybearsoftware.com/implementing-a-dynamic-array

正しい。 stdarg.hを使用してください。ウィキペディア例Cで連結するアレイの場合(https://en.wikipedia.org/wiki/Variadic_function

のためにポイントアウトをありがとう、2501 @How can I concatenate two arrays in C?

+0

私はここでダウンした有権者ではありませんが、動的メモリ割り当てを使用することは、ベリジアン関数を使用することと何が関係していますか? ( 'bytes bytesConcat(bytes fmt、...);') – ryyker

+0

私は結果の配列が可変長であると思います。したがって、動的メモリ割り当てです。引数ごとに配列が大きくなり、最初に関数に渡される引数の数は決まっていません。 –

+0

問題が解決しました。ありがとうございます。 – ant

関連する問題