在c中将向量转换为字符串的问题

时间:2022-09-01 11:31:49

I am trying to convert a vector into a string using the following function.

我试图使用以下函数将向量转换为字符串。

char* my_vect2str(char** input)
{
    int i;
    char* ret = (char*)xmalloc(sizeof(char*));
    for(i=0; input[i] != NULL; i++)
    {
        if(*input[i] == '\0')
            ret[i] = ' ';
        else
            ret[i] = *input[i];
    }
    ret[i] = '\0';
    return ret;
}

This appears to be getting just the first character of each string in the vector. How do I alter my for loop to get this working properly? Thanks!

这似乎只是向量中每个字符串的第一个字符。如何更改for循环以使其正常工作?谢谢!

2 个解决方案

#1


0  

Your malloc should be the size of the pointer contents, not the pointer itself. You also don't need to cast the malloc void *. You need an inner loop counter in order to iterate through both dimensions of you pointer. This should work:

malloc应该是指针内容的大小,而不是指针本身的大小。您也不需要强制转换malloc void *。你需要一个内部循环计数器来迭代你指针的两个维度。这应该工作:

char* my_vect2str(char** input)
{
    int i;
    int count = 0;
    char* ret = (char*)malloc(sizeof(char*)); // should be a larger size
    for(i=0; input[i] != NULL; i++)
    {
        int j = 0;
        while(1){
            if(input[i][j] == '\0'){
                ret[count++] = ' ';
                break;
            }else{
                ret[count++] = input[i][j];
            }
            j++;
        }
    }
    ret[count] = '\0';
    return ret;
}

#2


0  

The first loop calculates the total size of the strings in input. Then, the space is allocated and the strings are concatenated to ret.

第一个循环计算输入中字符串的总大小。然后,分配空间并将字符串连接到ret。

char* my_vect2str(char** input)
{
    int i, j, k = 0;
    char* ret;
    int size = 0;
    int len;
    char* inp = input[k++];

    while (inp != NULL) {
        size += strlen(inp);
        inp = input[k++];
    }

    ret = malloc((size * sizeof(char)) + 1);
    memset(ret, 0, size + 1);

    i = 0;
    j = 0;
    while (i < size) {
        if (input[j] != NULL) {
            len = strlen(input[j]);
            memcpy(&ret[i], input[j], len);
            i += len;
        }
        ++j;
    }

    return ret;
}

#1


0  

Your malloc should be the size of the pointer contents, not the pointer itself. You also don't need to cast the malloc void *. You need an inner loop counter in order to iterate through both dimensions of you pointer. This should work:

malloc应该是指针内容的大小,而不是指针本身的大小。您也不需要强制转换malloc void *。你需要一个内部循环计数器来迭代你指针的两个维度。这应该工作:

char* my_vect2str(char** input)
{
    int i;
    int count = 0;
    char* ret = (char*)malloc(sizeof(char*)); // should be a larger size
    for(i=0; input[i] != NULL; i++)
    {
        int j = 0;
        while(1){
            if(input[i][j] == '\0'){
                ret[count++] = ' ';
                break;
            }else{
                ret[count++] = input[i][j];
            }
            j++;
        }
    }
    ret[count] = '\0';
    return ret;
}

#2


0  

The first loop calculates the total size of the strings in input. Then, the space is allocated and the strings are concatenated to ret.

第一个循环计算输入中字符串的总大小。然后,分配空间并将字符串连接到ret。

char* my_vect2str(char** input)
{
    int i, j, k = 0;
    char* ret;
    int size = 0;
    int len;
    char* inp = input[k++];

    while (inp != NULL) {
        size += strlen(inp);
        inp = input[k++];
    }

    ret = malloc((size * sizeof(char)) + 1);
    memset(ret, 0, size + 1);

    i = 0;
    j = 0;
    while (i < size) {
        if (input[j] != NULL) {
            len = strlen(input[j]);
            memcpy(&ret[i], input[j], len);
            i += len;
        }
        ++j;
    }

    return ret;
}