本文介绍了2复制到阵列1阵列,在C PROG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以复制2个独立的二维数组到1阵列,下面我明白我的意思描述:

我有1阵列:A,B,C
我有第二个数组:D,E,F

我想第三排有两个以上的数组:
第三阵:A,B,C,D,E,F

到目前为止,我的code只是取值为阵列和打印第三阵列时我注释掉:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释主(){
    INT I,J,计数;    炭AR1 [3] [10] = {一,B,C};
    CHAR AR2 [3] [10] = {D,E,F};
    炭AR3 [6] [10];    对于(I = 0; I&下; 3;我++){
        的printf(%S \\ n,AR1 [I]);
    }
    对于(I = 0; I&下; 3;我++){
        的printf(%S \\ n,AR2 [I]);
    }
    的printf(新阵:\\ n);
//对于(I = 0; I&10 6;我+ +)
//的printf(%S \\ t \\ n,AR3 [I]);
}


解决方案

作为阵列的最右边的尺寸相等,则在一个复制两个数组最简单的方法是以下

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    炭AR1 [3] [10] = {一,B,C};
    CHAR AR2 [3] [10] = {D,E,F};
    炭AR3 [6] [10];    的memcpy(AR3,AR1,sizeof的(AR1));
    的memcpy(AR3 + 3,AR2,sizeof的(AR2));    用于(为size_t我= 0;我6;;我++)
    {
        看跌期权(AR3 [I]);
    }    返回0;
}

的输出是

  A
b
C
ð
Ë
F

另一种方法是给每个separatly复制使用功能的strcpy

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    炭AR1 [3] [10] = {一,B,C};
    CHAR AR2 [3] [10] = {D,E,F};
    炭AR3 [6] [10];    为size_t J = 0;
    为(为size_t I = 0; I&下; 3;我+ +,J ++)
    {
        的strcpy(AR3 [J],AR1 [I]);
    }    为(为size_t I = 0; I&下; 3;我+ +,J ++)
    {
        的strcpy(AR3 [J],AR2 [I]);
    }    用于(为size_t我= 0;我6;;我++)
    {
        看跌期权(AR3 [I]);
    }    返回0;
}

的输出将是与上述相同的

  A
b
C
ð
Ë
F

How can i copy 2 seperate 2D arrays into 1 array, i have described below what i mean:

I have 1 array: a, b, cI have a second array: d, e, f

I want the third array to have both the above arrays:3rd array: a, b, c, d, e, f

So far my code is just taking values for both arrays and i commented out when printing the 3rd array:

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

int main(){
    int i,j,count;

    char ar1[3][10]={"a","b","c"};
    char ar2[3][10]={"d","e","f"};
    char ar3[6][10];

    for (i=0;i<3;i++){
        printf("%s\n",ar1[i]);
    }
    for (i=0;i<3;i++){
        printf("%s\n",ar2[i]);
    }
    printf('new array:\n');
//  for (i=0;i<6;i++)
//      printf("%s\t\n",ar3[i]);
}
解决方案

As the right most dimensions of the arrays are equal then the simplest way to copy two arrays in one is the following

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

int main(void)
{
    char ar1[3][10] = { "a", "b", "c" };
    char ar2[3][10] = { "d", "e", "f" };
    char ar3[6][10];

    memcpy( ar3, ar1, sizeof( ar1 ) );
    memcpy( ar3 + 3, ar2, sizeof( ar2 ) );

    for ( size_t i = 0; i < 6; i++ )
    {
        puts( ar3[i] );
    }

    return 0;
}

The output is

a
b
c
d
e
f

The other approach is to copy each string separatly using function strcpy

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

int main(void)
{
    char ar1[3][10] = { "a", "b", "c" };
    char ar2[3][10] = { "d", "e", "f" };
    char ar3[6][10];

    size_t j = 0;
    for ( size_t i = 0; i < 3; i++, j++ )
    {
        strcpy( ar3[j], ar1[i] );
    }

    for ( size_t i = 0; i < 3; i++, j++ )
    {
        strcpy( ar3[j], ar2[i] );
    }

    for ( size_t i = 0; i < 6; i++ )
    {
        puts( ar3[i] );
    }

    return 0;
}

The output will be the same as above

a
b
c
d
e
f

这篇关于2复制到阵列1阵列,在C PROG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:37