简体   繁体   English

C:将可变长度的2D数组转换为1d字符串

[英]C: Convert variable length 2D array into 1d string

Consider a 2D array of type char**. 考虑类型为char **的2D数组。 This array has a finite number of rows (number of substrings), but variable column length (variable substring length). 该数组具有有限的行数(子字符串数),但列长度可变(子字符串长度可变)。

I would like to create a function that takes this array and number of rows (substrings) as parameters, and returns a single string that consists of each ordered substring (row). 我想创建一个函数,将这个数组和行数(子字符串)作为参数,并返回一个包含每个有序子字符串(行)的字符串。 It it a simple concatenation. 它是一个简单的串联。 This is the opposite behaviour of this question . 这是该问题的相反行为。

I have written the following code to do so, but it only works if each substring is the same length (constant column length), and therefore I pass the maximum column length as a function parameter: 我已经编写了以下代码来执行此操作,但是仅当每个子字符串的长度相同(恒定列长度)时,它才有效,因此我将最大列长度作为函数参数传递:

char* cat2dCharMat(char** A, int m, int n){

    char* temp = malloc((m*n+1));
    int length = 0;
    for(int i = 0; i < m;++i){
        memcpy(&temp[n*i], A[i],strlen(A[i]));
        length += (int)strlen(A[i]);
    }
    temp[length] = '\0';
    printf("Length of concatenated string is %d chars.\n",strlen(temp));
    return temp;
}

How can I make this more general to take in many columns lengths? 如何使它更通用,以容纳许多列长度? I also wrote a main() to accompany this function for a complete, minimum, verifiable example (forgive the 2D array initialization -- I found that using array = {"hello", "world!"} did not work): 我还编写了一个main()来伴随此函数,以获取一个完整的,最小的,可验证的示例(原谅2D数组初始化-我发现使用array = {“ hello”,“ world!”}无效):

int main(void){
    char** array2 = (char**)malloc((3)*sizeof(char*));
    for(int i = 0; i < 3; ++i){
        array2[i] = (char*)malloc(4*sizeof(char));
        for(int j = 0; j < 3;++j){
            if(j==0 && i==0)
                array2[i][j] = '0';
            else if(j==1 && i==0)
                array2[i][j] = '8';
            else if(j==2 && i==0)
                array2[i][j] = '7';
            else if(j==0 && i==1)
                array2[i][j] = '4';
            else if(j==1 && i==1)
                array2[i][j] = '9';
            else if(j==2 && i==1)
                array2[i][j] = '5';
        }
    }
    char** array1 = (char**)malloc((3)*sizeof(char*));
    for(int i = 0; i < 3; ++i){
        if(i == 0){
            array1[i] = malloc(4);
            for(int j = 0; j < 3;++j){
                if(j==0 && i==0)
                    array1[i][j] = '0';
                else if(j==1 && i==0)
                    array1[i][j] = '8';
                else if(j==2 && i==0)
                    array1[i][j] = '7';
            }
        }else{
            array1[i] = malloc(5);
            for(int j = 0; j < 4;++j){
                if(j==0 && i==0)
                    array1[i][j] = '0';
                else if(j==1 && i==1)
                    array1[i][j] = '8';
                else if(j==2 && i==1)
                    array1[i][j] = '7';
                else if(j==3 && i==1)
                    array1[i][j] = '7';
            }
        }
    }
    array1[0][3] = '\0';
    array1[1][4] = '\0';
    char* array1cat = cat2dCharMat(array1,2,4);
    char* array2cat = cat2dCharMat(array2,2,3);
    printf("Testing cat2dCharMat()...\n\n");
    printf("Case 1: {\"087\",\"495\"}\n");
    printf("Expected Result: 087495\n");
    printf("Actual Result:   %s\n", array2cat);
    printf("Case 2:{\"087\",\"0877\"}\n");
    printf("Expected Result: 0870877\n");
    printf("Actual Result:   %s\n", array1cat);
    }

I didn't understand well the second parameter you wish for your function ("number of rows (substrings)"), so here is an idea concatenating the n first strings of your "array" (which is not an array, C: differences between char pointer and array ) 我不太了解您希望函数使用的第二个参数(“行数(子字符串)”),因此这是将“数组”的n第一个字符串(不是数组, C:差异)连接在一起的想法在char指针和array之间

Please take note that it's not an safe function at all, since it does not check the value you give to n isn't to big for your variable (it would provoke a segmentation fault); 请注意, 它根本不是一个安全函数 ,因为它不会检查您为n赋予的值对于变量来说不是很大(这会引发分段错误); take care of this point when implementing your own function in your own code. 在自己的代码中实现自己的功能时,请注意这一点。

char * concatenate_these_strings (char ** my_strings ,unsigned int n){
    int i,size = 1;
    for ( i=0 ; i<n ; i++){
            size += strlen (my_strings[i]);
    }
    char * my_result = malloc (size);
    for ( i=0 ; i<n ; i++){
        strcat (my_result,my_strings[i]);
    }
    return my_result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM