C - Char Array Array

I am trying to work with an example in book K and R for this topic, but I'm afraid.

I need an array of Char arrays, as a result, each element of the Father array points to an array of characters (string). Basically, I read from a file, lines at a time, saving each line in an array, and then trying to save this array into another array, which I can then sort through qsort.

But I can’t handle it anywhere! Anyhelp on my code is very appreciated, that is, where to go, where I come from!

EDIT: the problem is that the print function does not print my words, which should be in an array of arrays, instead just print garbage, the main problem is that I'm not sure if I delete links all correctly or not, regardless from whether I am adding it to the array of arrays correctly, etc.

Sincerely.

#define MAXLINES 5000 /* max no. lines to be stored */ #define MAXLEN 1000 /* max length of single line */ char *lineptr[MAXLINES]; void writelines(char *lineptr[], int nlines); int main(int argc, char *argv[]) { int nlines = 0, i, j, k; char line[MAXLEN]; FILE *fpIn; fpIn = fopen(argv[1], "rb"); while((fgets(line, 65, fpIn)) != NULL) { j = strlen(line); if (j > 0 && (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j > 8) { lineptr[nlines++] = line; } } for(i = 0; i < nlines; i++) printf("%s\n", lineptr[i] ); return 0; } 
+4
source share
2 answers

Dan definitely found one error, identical memory . But I think there are more errors here:

 while((fgets(line, 65, fpIn)) != NULL) { 

Why only 65 ? You have a MAXLEN space that you could work with, you can also allow your input a bit longer.

  j = strlen(line); if (j > 0 && (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j > 8) { lineptr[nlines++] = line; } } 

Why exactly j > 8 ? Should you throw short lines? Remember to free up memory for the string in this case, as soon as you move on to the dynamic allocation that Dan offers.

Update

ott recommends strdup(3) - this will fit seamlessly into your existing system:

 while((fgets(line, 65, fpIn)) != NULL) { j = strlen(line); if (j > 0 && (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j > 8) { lineptr[nlines++] = strdup(line); } } 

Dan recommended calloc(3) , this will only be a bit more work:

 line = calloc(MAXLINE, sizeof char); while((fgets(line, 65, fpIn)) != NULL) { j = strlen(line); if (j > 0 && (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j > 8) { lineptr[nlines++] = line; line = calloc(MAXLINE, sizeof char); } } 

Of course, both of these approaches will explode if a memory failure fails - error checking returns from memory allocation - it is always a good idea. And also there is something completely invincible about the second mechanism.

+4
source

The problem is that line[MAXLEN] is an automatic variable, and therefore every time through a while loop it refers to the same array. You must dynamically allocate line every time through a while loop ( line = calloc(MAXLEN, sizeof(char)) before calling fgets ). Otherwise, fgets always written to the same memory location, and lineptr always points to the same array.

+5
source

Source: https://habr.com/ru/post/1388393/


All Articles