C-使用for循环在一个数组中连接2个数组

C-使用for循环在一个数组中连接2个数组,c,arrays,loops,for-loop,C,Arrays,Loops,For Loop,我想在C语言中运行一个For循环来连接,然后将结果复制到一个新数组中 // Simple array with numbers to be appended at the end of the array "type" below char numbers[20][2]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"}; //after each of

我想在C语言中运行一个For循环来连接,然后将结果复制到一个新数组中

    // Simple array with numbers to be appended at the end of the array "type" below

char numbers[20][2]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"};

//after each of these words i want to append the number found above.

char type[10][30]={"rizi","makaronia","kafes","giaourti","feta","avga","sampuan","rouxa","aporipantiko","aposmitiko"};

//This is the array i wish to add the results. This way i will create 20 of each type

char random_type [20][30];

    int i,j;
    for(i = 0; i < 10; i++)
    {
        for (j = 0; i < 20; j++)
        {
            strcpy(random_type[i][j],type[j]);
        }

    }
//在下面的数组“type”末尾附加数字的简单数组
字符数[20][2]={“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”};
//在每一个单词后面,我想加上上面的数字。
字符类型[10][30]={“rizi”、“makaronia”、“kafes”、“giaourti”、“feta”、“avga”、“sampuan”、“rouxa”、“aporipantiko”、“aposmitiko”};
//这是我希望添加结果的数组。这样我将创建每种类型的20个
字符随机_型[20][30];
int i,j;
对于(i=0;i<10;i++)
{
对于(j=0;i<20;j++)
{
strcpy(随机_型[i][j],型[j]);
}
}

可以进行一些简化,例如不需要连续数字数组。有一些错误,例如@iharob和@Jasper指出,OP使用
strcpy()
写入二维字符数组的每个字符,这实际上是一个一维字符串数组

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

#define WORDS   10
#define ANSWERS 20
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [ANSWERS][35];
    int i, word, numb;
    srand ((unsigned)time(NULL));
    for(i=0; i<ANSWERS; i++) {
        numb = rand() % NUMBERS;
        word = rand() % WORDS;
        sprintf(random_type[i], "%s %d", type[word], numb);
    }
    for(i=0; i<ANSWERS; i++) {
        printf ("%s\n", random_type[i]);
    }
    return 0;
}
虽然,也许OP的意思是用每个数字来排列每个单词,在这种情况下,我提供了这个

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

#define WORDS   10
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [WORDS][NUMBERS][35];
    int i, j;
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            sprintf(random_type[i][j], "%s %d", type[i], j);
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            printf ("%s\n", random_type[i][j]);
    return 0;
}
#包括
#包括
#定义单词10
#定义数字20
int main()
{
//附加数字的词
字符类型[单词][30]={“rizi”、“makaronia”、“kafes”、“giaourti”、“feta”,
“avga”、“sampuan”、“rouxa”、“aporipantiko”、“aposmitiko”};
//这是用于创建结果的数组。
字符随机_型[字][数][35];
int i,j;

for(i=0;i“10”将无法放入
char[2]
数组,因为终止的是零字节。我不清楚
random\u type
元素的期望值是什么。无限循环->
for(j=0;i<20;j++)
,以及
random\u type[i][j]
?@Jasper我已经设置了char 20][2]我看到它是这样的。不是吗?一个长度为2的字符串需要一个大小为3的字符数组来包含它,以及它的终止符。谢谢!我真正需要的是在一个数组中添加总共200个结果的信息,这样我就可以将它传输到一个结构。@falsobuio如果我的回答有用,请“接受”谢谢。@falsobuio我回答的第二部分在一个数组中总共创建了200个结果,由于大小原因,我没有打印它们。@falsobuio我添加了第三部分,创建了一个包含200个字符串的字符串数组。
#include <stdio.h>
#include <stdlib.h>

#define WORDS   10
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [WORDS][NUMBERS][35];
    int i, j;
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            sprintf(random_type[i][j], "%s %d", type[i], j);
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            printf ("%s\n", random_type[i][j]);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

#define WORDS   10
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [WORDS*NUMBERS][35];
    int i, j, k=0;
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            sprintf(random_type[k++], "%s %d", type[i], j);
    for(k=0; k<WORDS*NUMBERS; k++)
            printf ("%s\n", random_type[k]);
    return 0;
}