1
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. 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
NewCoder
  • 183
  • 1
  • 14
  • 1
    `sizeof(char*)*sizeof(countNewLine)`..looks suspicious. – Sourav Ghosh Mar 10 '15 at 10:00
  • 1
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family. – Sourav Ghosh Mar 10 '15 at 10:01
  • 1
    The loop will never terminate with `buffer[rowTrack] != '\n' || buffer[rowTrack] != '\0';` it should use `&&` and also, you have not allocated memory for each row of `*transfer[]`. – Weather Vane Mar 10 '15 at 10:12
  • @WeatherVane Desktop/test/test/test/main.c:14:13: Definition of variable with array type needs an explicit size or an initializer for `char *transfer[]` – NewCoder Mar 10 '15 at 10:16
  • So, to be clear, you want to *split* string by newlines? – hyde Mar 10 '15 at 10:42

1 Answers1

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

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 ( 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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56