将一维字符串数组转换为c中的二维字符串数组

标签 c arrays string malloc

char   *buffer;        /* holds the file contents. */
char   **transfer;
unsigned long    countNewLine = 0;
size_t  rowTrack;
size_t  columnTrack;

// assume countNewLine is 12

buffer_size = BUFSIZ;
buffer = malloc(buffer_size);
transfer = (char**)malloc(sizeof(char*)*sizeof(countNewLine));

columnTrack = 0;


while ( columnTrack < countNewLine ) {

    for ( rowTrack = 0; buffer[rowTrack] != '\n' || buffer[rowTrack] != '\0'; rowTrack++ )
        transfer[columnTrack][rowTrack] = buffer[rowTrack];

        columnTrack++;
}

我正在尝试将一维字符串数组转换为二维。我不知道我的错误。感谢您的解决方案。

调试器结果:

    buffer  char *  "first\nsecond\nthird\nfourth\nfifth\nsixth\nseventh\n
              eighth\nninth\ntenth\neleventh\ntwelfth\nthirteenth"  0x0000000100801200
    *buffer char    'f' 'f'
    countNewLine    unsigned long   12  12
    transfer    char ** 0x100105520 0x0000000100105520
    *transfer   char *  NULL    0x0000000000000000
    rowTrack    size_t  0   0
    columnTrack size_t  0   0

最佳答案

您的代码中有几个错误。在这一行

transfer = (char**)malloc(sizeof(char*)*sizeof(countNewLine));

第二个sizeof()不正确,该行应该是

transfer = malloc(sizeof(char*) * countNewLine);

接下来,你没有为每一行分配内存,可能是这样的

for ( rowTrack = 0; rowTrack < countNewLine; rowTrack++)
    transfer[rowTrack] = malloc(BUFSIZ);    // or whatever the line length is

for 循环将永远不会通过使用 || 终止

for ( rowTrack = 0; buffer[rowTrack] != '\n' || buffer[rowTrack] != '\0'; rowTrack++ )

应该是

for ( rowTrack = 0; buffer[rowTrack] != '\n' && buffer[rowTrack] != '\0'; rowTrack++ )

最后,我认为您在此语句中颠倒了行和列索引:

transfer[columnTrack][rowTrack] = buffer[rowTrack];

有一种更简洁的方式来拆分字符串,如下所示:

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

#define BUFSIZ 512

int main(void) { 

    char *buffer;
    char *sptr;
    char **transfer = NULL;
    int rows = 0, row;

    /* ... load the file contents */
    buffer = malloc(BUFSIZ);
    strcpy (buffer, "first\nsecond\nthird\nfourth\nfifth\nsixth\nseventh\neighth\nninth\ntenth\neleventh\ntwelfth\nthirteenth");

    /* split the input */
    sptr = strtok(buffer, "\r\n");
    while (sptr) {
        transfer = realloc(transfer, (rows+1) * sizeof(char*));  // expand string array
        transfer[rows] = malloc(1+strlen(sptr));    // memory for string
        strcpy (transfer[rows], sptr);              // copy the token
        rows++;
        sptr = strtok(NULL, "\r\n");
    }

    /* show array */
    printf ("Showing %d rows\n", rows);
    for (row=0; row<rows; row++) {
        printf ("%s\n", transfer[row]);
        free (transfer[row]);                       // release mem
    }

    free (transfer);                                // release mem
    free (buffer);                                  // release mem
    return 0;
}

程序输出:

Showing 13 rows
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
eleventh
twelfth
thirteenth

关于将一维字符串数组转换为c中的二维字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28960654/

相关文章:

python - 将 Linux 内核 add_key 和 keyctl 系统调用与组 key 环一起使用

C 中指针变量的冲突类型(错误)

arrays - 查找总和小于给定值的最大元素(连续)?

python - numpy.repeat 的反义词是什么?

java - 我需要使用子字符串,但我需要包含第二个参数

c - 使用 BFS 改进 8-Puzzle

sql - 如何在 Tarantool 中从 SQL 调用 C 函数?

javascript - 将新对象添加到现有对象数组 jquery/javascript

php - 使用 MYSQL PHP 按每个单词而不是完整的字符串搜索

java - 在Java中,byte[]与string作为方法参数有什么优缺点?