8

How can I include the elements of array X and Y in to array total in C language ? can you please show with an example.

X = (float*) malloc(4);
Y = (float*) malloc(4);
total = (float*) malloc(8);

for (i = 0; i < 4; i++)
{
    h_x[i] = 1;
    h_y[i] = 2;
}

//How can I make 'total' have both the arrays x and y
//for example I would like the following to print out 
// 1, 1, 1, 1, 2, 2, 2, 2

for (i = 0; i < 8; i++)
    printf("%.1f, ", total[i]);
4

8 回答 8

47

您现有的代码分配了错误的内存量,因为它根本没有考虑sizeof(float)到。

除此之外,您可以将一个数组附加到另​​一个数组memcpy

float x[4] = { 1, 1, 1, 1 };
float y[4] = { 2, 2, 2, 2 };

float* total = malloc(8 * sizeof(float)); // array to hold the result

memcpy(total,     x, 4 * sizeof(float)); // copy 4 floats from x to total[0]...total[3]
memcpy(total + 4, y, 4 * sizeof(float)); // copy 4 floats from y to total[4]...total[7]
于 2012-08-13T10:29:23.630 回答
3
for (i = 0; i < 4; i++)
{
    total[i]  =h_x[i] = 1;
    total[i+4]=h_y[i] = 2;
}
于 2012-08-13T10:43:05.007 回答
2

当您知道它们的大小时,一种连接两个 C 数组的方法。

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

#define ARRAY_CONCAT(TYPE, A, An, B, Bn) \
(TYPE *)array_concat((const void *)(A), (An), (const void *)(B), (Bn), sizeof(TYPE));

void *array_concat(const void *a, size_t an,
               const void *b, size_t bn, size_t s)
{
    char *p = malloc(s * (an + bn));
    memcpy(p, a, an*s);
    memcpy(p + an*s, b, bn*s);
    return p;
}

// testing
const int a[] = { 1, 1, 1, 1 };
const int b[] = { 2, 2, 2, 2 };

int main(void)
{
    unsigned int i;

    int *total = ARRAY_CONCAT(int, a, 4, b, 4);

    for(i = 0; i < 8; i++)
        printf("%d\n", total[i]);

    free(total);
    return EXIT_SUCCCESS;
}
于 2012-08-13T10:35:20.440 回答
0

我想我会添加这个,因为我发现过去有必要将值附加到 C 数组(如NSMutableArray在 Objective-C 中)。此代码管理一个 Cfloat数组并向其附加值:

static float *arr;
static int length;

void appendFloat(float);

int main(int argc, const char * argv[]) {

    float val = 0.1f;
    appendFloat(val);

    return 0;
}

void appendFloat(float val) {
    /*
     * How to manage a mutable C float array
     */
    // Create temp array
    float *temp = malloc(sizeof(float) * length + 1);
    if (length > 0 && arr != NULL) {
        // Copy value of arr into temp if arr has values
        memccpy(temp, arr, length, sizeof(float));
        // Free origional arr
        free(arr);
    }
    // Length += 1
    length++;
    // Append the value
    temp[length] = val;
    // Set value of temp to arr
    arr = temp;
}
于 2015-05-25T13:28:55.787 回答
0

为什么不使用这样的简单逻辑呢?

在此处输入图像描述

#include<stdio.h>  
  
#define N 5  
#define M (N * 2)  
  
int main()  
{  
    int a[N], b[N], c[M], i, index = 0;  
  
    printf("Enter %d integer numbers, for first array\n", N);  
    for(i = 0; i < N; i++)  
        scanf("%d", &a[i]);  
  
    printf("Enter %d integer numbers, for second array\n", N);  
    for(i = 0; i < N; i++)  
            scanf("%d", &b[i]);  
  
    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);  
    for(i = 0; i < N; i++)  
        c[index++] = a[i];  
  
    for(i = 0; i < N; i++)  
        c[index++] = b[i];  
  
    printf("\nElements of c[%d] is ..\n", M);  
    for(i = 0; i < M; i++)  
        printf("%d\n", c[i]);  
  
    return 0;  
} 

结果数组大小必须等于数组 a 和 b 的大小。

资料来源: 连接两个数组的 C 程序

于 2020-08-15T19:56:17.890 回答
0

这是连接两个或多个静态分配数组的解决方案。静态分配的数组是在编译时定义长度的数组。sizeof运算符返回这些数组的大小(以字节为单位):

char Static[16];               // A statically allocated array
int n = sizeof(Static_array);  // n1 == 16

我们可以使用该运算符sizeof来构建一组将连接两个或多个数组的宏,并可能返回数组的总长度。

我们的宏:

#include <string.h>

#define cat(z, a)          *((uint8_t *)memcpy(&(z), &(a), sizeof(a)) + sizeof(a))
#define cat1(z, a)         cat((z),(a))
#define cat2(z, a, b)      cat1(cat((z),(a)),b)
#define cat3(z, a, b...)   cat2(cat((z),(a)),b)
#define cat4(z, a, b...)   cat3(cat((z),(a)),b)
#define cat5(z, a, b...)   cat4(cat((z),(a)),b)
// ... add more as necessary
#define catn(n, z, a ...)  (&cat ## n((z), a) - (uint8_t *)&(z)) // Returns total length

使用示例:

char      One[1]   = { 0x11 };
char      Two[2]   = { 0x22, 0x22 };
char      Three[3] = { 0x33, 0x33, 0x33 };
char      Four[4]  = { 0x44, 0x44, 0x44, 0x44 };
char      All[10];
unsigned  nAll = catn(4, All, One, Two, Three, Four);

然而,由于我们定义宏的方式,我们可以连接任何类型的对象,只要sizeof返回它们的大小。例如:

char      One      = 0x11;                                // A byte
char      Two[2]   = { 0x22, 0x22 };                      // An array of two byte
char      Three[]  = "33";                                // A string ! 3rd byte = '\x00'
struct {
    char  a[2];
    short  b;
}         Four     = { .a = { 0x44, 0x44}, .b = 0x4444 }; // A structure
void *    Eight    = &One;                                // A 64-bit pointer
char      All[18];
unsigned  nAll     = catn(5, All, One, Two, Three, Four, Eight);

使用常量字面量,也可以使用这些宏来连接常量、函数的结果,甚至是常量数组:

// Here we concatenate a constant, a function result, and a constant array
cat2(All,(char){0x11},(unsigned){some_fct()},((uint8_t[4]){1,2,3,4}));
于 2018-10-20T14:21:34.667 回答
0

我喜欢乔恩的回答。就我而言,我不得不使用静态解决方案。因此,如果您被迫不使用动态内存分配:

int arr1[5] = {11,2,33,45,5};
int arr2[3] = {16,73,80};
int final_arr[8];

memcpy(final_arr, arr1, 5 * sizeof(int));
memcpy(&final_arr[5], arr2, 3 * sizeof(int));

for(int i=0; i<(sizeof(final_arr)/sizeof(final_arr[0]));i++){
    printf("final_arr: %i\n", final_arr[i]);
}
于 2018-10-26T08:51:32.437 回答
0

可能这很简单。

#include <stdio.h>

int main()
{
    int i,j,k,n,m,total,a[30],b[30],c[60];
    //getting array a
    printf("enter size of array A:");
    scanf("%d",&n);
    printf("enter %d elements \n",n);
    for(i=0;i<n;++i)
    {scanf("%d",&a[i]);}

    //getting aaray b
    printf("enter size of array b:");
    scanf("%d",&m);
    printf("enter %d elements \n",m);
    for(j=0;j<m;++j)
    {scanf("%d",&b[j]);}

    total=m+n;
    i=0,j=0;

    //concating starts    
    for(i=0;i<n;++i)
    {
       c[i]=a[i];
    }
    for(j=0;j<m;++j,++n)
    {
       c[n]=b[j];
    }

    printf("printing c\n");
    for(k=0;k<total;++k)
    {printf("%d\n",c[k]);}
}
于 2015-12-12T09:08:01.623 回答