[英]Convert one dimensional string array to two dimensional string array in c
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++;
}
I'm trying to convert 1D string array to 2D. 我正在尝试将1D字符串数组转换为2D。 I don't have any idea about my mistake.
我对我的错误一无所知。 Thank you for solution.
谢谢您的解决方案。
Debugger result: 调试器结果:
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
There are several errors in your code. 您的代码中有几个错误。 In this line
在这条线
transfer = (char**)malloc(sizeof(char*)*sizeof(countNewLine));
the second sizeof()
is incorrect, the line should be 第二个
sizeof()
不正确,该行应为
transfer = malloc(sizeof(char*) * countNewLine);
Next, you did not allocate memory for each row, which could be something like this 接下来,您没有为每一行分配内存,可能是这样的
for ( rowTrack = 0; rowTrack < countNewLine; rowTrack++)
transfer[rowTrack] = malloc(BUFSIZ); // or whatever the line length is
The for
loop will never terminate by using ||
for
循环永远不会使用||
终止
for ( rowTrack = 0; buffer[rowTrack] != '\n' || buffer[rowTrack] != '\0'; rowTrack++ )
should be 应该
for ( rowTrack = 0; buffer[rowTrack] != '\n' && buffer[rowTrack] != '\0'; rowTrack++ )
And lastly, I think you have your row and column indexing reversed in this statement: 最后,我认为您在此语句中反转了行和列索引:
transfer[columnTrack][rowTrack] = buffer[rowTrack];
There is a much cleaner way to split your string, like this: 有一种更清晰的方式来拆分字符串,如下所示:
#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;
}
Program output: 程序输出:
Showing 13 rows
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
eleventh
twelfth
thirteenth
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.