如何在C中将数组拆分为两个数组

发布于 2024-11-04 02:44:18 字数 220 浏览 3 评论 0原文

假设我在 C 中有一个数组,

int array[6] = {1,2,3,4,5,6}

我如何将其拆分为

{1,2,3}

{4,5,6}

这可以使用 memcpy 吗?

谢谢你,

诺诺诺

Say i have an array in C

int array[6] = {1,2,3,4,5,6}

how could I split this into

{1,2,3}

and

{4,5,6}

Would this be possible using memcpy?

Thank You,

nonono

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2024-11-11 02:44:18

当然。最简单的解决方案是使用 malloc 分配两个新数组,然后使用 memcpy 将数据复制到这两个数组中。

int array[6] = {1,2,3,4,5,6}
int *firstHalf = malloc(3 * sizeof(int));
if (!firstHalf) {
  /* handle error */
}

int *secondHalf = malloc(3 * sizeof(int));
if (!secondHalf) {
  /* handle error */
}

memcpy(firstHalf, array, 3 * sizeof(int));
memcpy(secondHalf, array + 3, 3 * sizeof(int));

但是,如果原始数组存在足够长的时间,您可能甚至不需要这样做。您可以通过使用指向原始数组的指针将数组“拆分”为两个新数组:

int array[6] = {1,2,3,4,5,6}
int *firstHalf = array;
int *secondHalf = array + 3;

Sure. The straightforward solution is to allocate two new arrays using malloc and then using memcpy to copy the data into the two arrays.

int array[6] = {1,2,3,4,5,6}
int *firstHalf = malloc(3 * sizeof(int));
if (!firstHalf) {
  /* handle error */
}

int *secondHalf = malloc(3 * sizeof(int));
if (!secondHalf) {
  /* handle error */
}

memcpy(firstHalf, array, 3 * sizeof(int));
memcpy(secondHalf, array + 3, 3 * sizeof(int));

However, in case the original array exists long enough, you might not even need to do that. You could just 'split' the array into two new arrays by using pointers into the original array:

int array[6] = {1,2,3,4,5,6}
int *firstHalf = array;
int *secondHalf = array + 3;
昵称有卵用 2024-11-11 02:44:18
// create space for 6 ints and initialize the first 6
int array[6] = {1,2,3,4,5,6};
// reserve space for two lots of 3 contiguous integers
int one[3], two[3]; 
// copy memory of the first 3 ints of array to one
memcpy(one, array, 3 * sizeof(int)); 
// copy 3 ints worth of memory from the 4th item in array onwards
memcpy(two, &array[3], 3 * sizeof(int)); 
// create space for 6 ints and initialize the first 6
int array[6] = {1,2,3,4,5,6};
// reserve space for two lots of 3 contiguous integers
int one[3], two[3]; 
// copy memory of the first 3 ints of array to one
memcpy(one, array, 3 * sizeof(int)); 
// copy 3 ints worth of memory from the 4th item in array onwards
memcpy(two, &array[3], 3 * sizeof(int)); 
自找没趣 2024-11-11 02:44:18

你不必把它们分开。如果有,

int *b = array + 3;

则有第二个数组。当您将数组传递给函数时,它无论如何都会变成指针。

You don't have to split them. If you have

int *b = array + 3;

you have the second array. When you pass an array to a function, it's turned into a pointer anyway.

淡笑忘祈一世凡恋 2024-11-11 02:44:18

了解 memcpy 工作原理的神奇之处,无需专门拆分数组。目标数组中所做的更改将自动转到源数组,反之亦然。

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

double *** double3d(long int dim1,long int dim2,long int dim3)
{
    long int i,j,k;
    double ***array;
    array=(double ***)malloc(dim1*sizeof(double **));
    for(i=0;i<dim1;i++)
    {
     array[i]=(double **)malloc(dim2*sizeof(double *));
     for(j=0;j<dim2;j++)
      array[i][j]=(double *)malloc(dim3*sizeof(double ));
    }
    return array;
}// end double3d

void summ(double ***A,double ***B, double ****C)
{
    int i ,j ,k;
    for(i=0;i<10;i++)
    for(j=0;j<5;j++)
    for(k=0;k<5;k++)
    (*C)[i][j][k]=A[i][j][k]+B[i][j][k];
}

void main()
{
   int i,j,k,nx,ny;
   double ***M1, ***M2, ***M3, ***M4,***M5,***M6;
   nx=5;ny=5;
    M1=double3d(10,nx,ny);
    M2=double3d(10,nx,ny);
    M3=double3d(10,nx,ny);
    M4=double3d(5,nx,ny);
    M5=double3d(5,nx,ny);
    M6=(double ***)malloc(10*sizeof(double **));

    for(i=0;i<10;i++)
    {
        for(j=0;j<nx;j++)
           for(k=0;k<ny;k++)
           {
           M1[i][j][k]=i;
           M2[i][j][k]=1;
           }
    }

// Note random values are in M4 and M5 as they are not initalised
    memcpy(M6,     M4, 5 * sizeof(double **));
    memcpy(M6+5,   M5, 5 * sizeof(double **));

    for(i=0;i<5;i++)
    {
       for(j=0;j<nx;j++)
          for(k=0;k<ny;k++)
          {
               M4[i][j][k]=200;
               M5[i][j][k]=700;
          }
    }

printf(" printing M6 Memcpy before addtion\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }
    // calling for non memcpy array
    summ(M1,M2,&M3); printf(" Non memcpy output last value : %f \n",M3[9][nx-1][ny-1]);
    // calling for memcpy
    summ(M1,M2,&M6); printf(" memcpy output last value : %f \n",M6[9][nx-1][ny-1]);
printf(" printing M6 Memcpy for two sets after addtion\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
    }
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }

    free(M6);// cleared M6

printf(" printing M4 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M4[0][j][k],M4[1][j][k],M4[2][j][k],M4[3][j][k],M4[4][j][k]);
       printf("\n");
    }

printf(" printing M5 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M5[0][j][k],M5[1][j][k],M5[2][j][k],M5[3][j][k],M5[4][j][k]);
       printf("\n");
    }
 }

See the magic how memcpy works, no need to exclusively split the arrays. The changes made in destination array are automatically go to source array and vise versa.

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

double *** double3d(long int dim1,long int dim2,long int dim3)
{
    long int i,j,k;
    double ***array;
    array=(double ***)malloc(dim1*sizeof(double **));
    for(i=0;i<dim1;i++)
    {
     array[i]=(double **)malloc(dim2*sizeof(double *));
     for(j=0;j<dim2;j++)
      array[i][j]=(double *)malloc(dim3*sizeof(double ));
    }
    return array;
}// end double3d

void summ(double ***A,double ***B, double ****C)
{
    int i ,j ,k;
    for(i=0;i<10;i++)
    for(j=0;j<5;j++)
    for(k=0;k<5;k++)
    (*C)[i][j][k]=A[i][j][k]+B[i][j][k];
}

void main()
{
   int i,j,k,nx,ny;
   double ***M1, ***M2, ***M3, ***M4,***M5,***M6;
   nx=5;ny=5;
    M1=double3d(10,nx,ny);
    M2=double3d(10,nx,ny);
    M3=double3d(10,nx,ny);
    M4=double3d(5,nx,ny);
    M5=double3d(5,nx,ny);
    M6=(double ***)malloc(10*sizeof(double **));

    for(i=0;i<10;i++)
    {
        for(j=0;j<nx;j++)
           for(k=0;k<ny;k++)
           {
           M1[i][j][k]=i;
           M2[i][j][k]=1;
           }
    }

// Note random values are in M4 and M5 as they are not initalised
    memcpy(M6,     M4, 5 * sizeof(double **));
    memcpy(M6+5,   M5, 5 * sizeof(double **));

    for(i=0;i<5;i++)
    {
       for(j=0;j<nx;j++)
          for(k=0;k<ny;k++)
          {
               M4[i][j][k]=200;
               M5[i][j][k]=700;
          }
    }

printf(" printing M6 Memcpy before addtion\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }
    // calling for non memcpy array
    summ(M1,M2,&M3); printf(" Non memcpy output last value : %f \n",M3[9][nx-1][ny-1]);
    // calling for memcpy
    summ(M1,M2,&M6); printf(" memcpy output last value : %f \n",M6[9][nx-1][ny-1]);
printf(" printing M6 Memcpy for two sets after addtion\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
    }
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }

    free(M6);// cleared M6

printf(" printing M4 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M4[0][j][k],M4[1][j][k],M4[2][j][k],M4[3][j][k],M4[4][j][k]);
       printf("\n");
    }

printf(" printing M5 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M5[0][j][k],M5[1][j][k],M5[2][j][k],M5[3][j][k],M5[4][j][k]);
       printf("\n");
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文