How can I fully reallocate a 2D array in C?

Cooper

Being concise, I want to read a file and store the content in a 2D array, where the first dimension refers to the line and the second dimension to the character in the respective line. I do not know the number of lines and columns on each line beforehand.

The code I tried is as follows.

P.S.: I know the variables's names are not that good XD

FILE *filepointer = fopen(commandlineargument.filtersfile, "r");
int numboflines = 0;
int numbofchars = 0;
char **array = malloc(numboflines + 1);
array[0] = malloc(numbofchars + 1);
char currentchar; 
while(fscanf(filepointer, "%c", &currentchar) != EOF)
{
    if(currentchar != '\n')
    {
        array[numboflines][numbofchars] = currentchar;
        numbofchars++;
        array[numboflines] = realloc(array[numboflines], numbofchars + 1);
    }
    else
    {
        array[numboflines][numbofchars] = '\0';
        numbofchars = 0;
        numboflines++;
        array = realloc(array,  numboflines + 1);
    }
}
array[numboflines] = '\0';
numboflines--;

With the code above, I can just get the value of array[0][0] and only before it gets reallocated two lines down (then array[0][0] points to null). I get Segmentation Fault later.

P.S.: I know the code above will only work properly if the last line ends with a \n

An example of the desired output:

File Content:

>Abcde
fghijk
>Lmno
pq

2D Array values:

array[0][0] = ">"
array[2][1] = "L"

And so on.

mcleod_ideafix

Your code is fine, but it has one omission and one mistake:

The mistake: memory space for each element of array is not a character, but a pointer to a character, so this line:

char **array = malloc(numboflines + 1);

should be rewritten as this:

char **array = malloc((numboflines + 1)*(sizeof *array));

The omission: after you end a line and realloc array, you must malloc the newly array element:

numboflines++;
array = realloc(array,  numboflines + 1);

Becomes:

numboflines++;
array = realloc(array,  (numboflines + 1)*(sizeof *array));
array[numboflines] = malloc(numbofchars + 1);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

C - How can I print random word inside game board (2d array)?

分類Dev

How can I filter a 2d array in Swift?

分類Dev

Why can't I make a copy of this 2d array in JS? How can I make a copy?

分類Dev

Why can't I make a copy of this 2d array in JS? How can I make a copy?

分類Dev

How can i pass a 2d array to JTable in Netbeans efficiently?

分類Dev

How can I check the mouse's position over a 2D array of images for a game?

分類Dev

How can I reallocate an ext4 file system in an extended partition without lose data?

分類Dev

How do I declare a 2d array in C++ using new?

分類Dev

How do I check the neighbors of the cells around me in a 2D array (C++)?

分類Dev

I can't assign a value to a 2d array

分類Dev

Can I trust a hard drive that has had to reallocate sectors?

分類Dev

How can I implement a zoom function in a 2D canvas?

分類Dev

How can i know when the library is fully loaded

分類Dev

How can I fully kill a program and/or python code running on Windows?

分類Dev

How to print a 2d character array C++

分類Dev

How to pass a 2d array from Python to C?

分類Dev

How to sort strings in an 2D array in C

分類Dev

How to delete this 2d dynamic array in c++

分類Dev

How can I convert an array name to an array pointer in C?

分類Dev

Unity3d - C# - How can I back up of values of an array?

分類Dev

C#: How can I add an Object into Array?

分類Dev

How to I check if every row in a 2D array has at least 2 elements?

分類Dev

Explain how it's possible to allocate and free memory for a 2d array in C with just these 2 lines:

分類Dev

How can I destructure this array?

分類Dev

How do I read from/write to a binary file in 2D Array form?

分類Dev

How do I get the function to print out a 2D array into two files in java?

分類Dev

How can I change the time between sprites in 2D animations on Unity?

分類Dev

How can I get the number of columns and rows from 2D list

分類Dev

How can I predict a 2d output in keras (label shape = 20, 42, 3)

Related 関連記事

  1. 1

    C - How can I print random word inside game board (2d array)?

  2. 2

    How can I filter a 2d array in Swift?

  3. 3

    Why can't I make a copy of this 2d array in JS? How can I make a copy?

  4. 4

    Why can't I make a copy of this 2d array in JS? How can I make a copy?

  5. 5

    How can i pass a 2d array to JTable in Netbeans efficiently?

  6. 6

    How can I check the mouse's position over a 2D array of images for a game?

  7. 7

    How can I reallocate an ext4 file system in an extended partition without lose data?

  8. 8

    How do I declare a 2d array in C++ using new?

  9. 9

    How do I check the neighbors of the cells around me in a 2D array (C++)?

  10. 10

    I can't assign a value to a 2d array

  11. 11

    Can I trust a hard drive that has had to reallocate sectors?

  12. 12

    How can I implement a zoom function in a 2D canvas?

  13. 13

    How can i know when the library is fully loaded

  14. 14

    How can I fully kill a program and/or python code running on Windows?

  15. 15

    How to print a 2d character array C++

  16. 16

    How to pass a 2d array from Python to C?

  17. 17

    How to sort strings in an 2D array in C

  18. 18

    How to delete this 2d dynamic array in c++

  19. 19

    How can I convert an array name to an array pointer in C?

  20. 20

    Unity3d - C# - How can I back up of values of an array?

  21. 21

    C#: How can I add an Object into Array?

  22. 22

    How to I check if every row in a 2D array has at least 2 elements?

  23. 23

    Explain how it's possible to allocate and free memory for a 2d array in C with just these 2 lines:

  24. 24

    How can I destructure this array?

  25. 25

    How do I read from/write to a binary file in 2D Array form?

  26. 26

    How do I get the function to print out a 2D array into two files in java?

  27. 27

    How can I change the time between sprites in 2D animations on Unity?

  28. 28

    How can I get the number of columns and rows from 2D list

  29. 29

    How can I predict a 2d output in keras (label shape = 20, 42, 3)

ホットタグ

アーカイブ