Take the 2-minute tour ×

The code below reads a text file in the same directory and prints each line out in the file in reverse order. For example, the text file I had read:
line 1
line 2
line 3

which would have printed out:
line 3
line 2
line 1

and it does, so what's the real problem? This question is not so much about fixing the functionality of some code I had a little help in writing but more about understanding the code and the logic behind what it does and how it accomplishes it.

I am very new to C (been learning for a few days) so please don't scream if I don't understand something relatively simple.


In the first while loop I have allocated memory blocks large enough to store a struct-type 'line' and defined the pointer 'l1' of type 'line' to hold the address of this allocated memory. Now here's the problem. On the line of code below this another memory block is allocated for a string with a buffer large enough to hold the size of each string read in the text file whose pointer is defined as l1->lines.

To me this seems illogical as 'l1' was defined only to be a pointer of type 'line' but on the second row of this while loop the 'l1->lines' suggests that I have declared a struct of the type 'line' named 'l1' and I am accessing one of its members ('lines') and am assigning the address of this struct member as the address of the newly allocated memory block. This means that 'l1' is a pointer to both a struct and also a pointer to a memory block.

From all this I can clearly say that my understanding of what is happening is skewed and is by no means correct. Can someone please enlighten me on what seems like a trivial problem?

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

typedef struct line {
  char *lines;
  struct line *prev;
} line;

FILE *file;   
int main() {

  line *tail = NULL;
  file = fopen("text.txt","r");
  char line1[80];

  while (fgets(line1,80,file)!=NULL) {
    line *l1 = malloc(sizeof(line));
    l1->lines = malloc(strlen(line1)+1);
    l1->prev = tail;
    tail = l1;

  line *current;
  current = tail;

  while (current != NULL) {
    current = current->prev;
  return 0;

share|improve this question

1 Answer 1

up vote 0 down vote accepted


I don't understand what the problem is with this. A struct resides in the memory and occupies some blocks of memory.

And all this code does, by the way, is building a linked list and then walking through it backwards.

share|improve this answer
Rather I meant that it points to an allocated memory block whose address is assigned to l1, which is an empty but allocated block. –  Shiri Jun 28 '13 at 9:54
@Shiri What do you think malloc(sizeof line) does? –  user529758 Jun 28 '13 at 9:55
Creates a memory block large enough to fit struct-type line and return it's address... right? –  Shiri Jun 28 '13 at 9:56
@Shiri Alrite. Exactly. And then returns a pointer to it. –  user529758 Jun 28 '13 at 9:58
so l1 then points to this allocated memory block as a type line pointer. However being a pointer it then has its members accessed (like a declared struct) via l1->lines which I don't understand if no real struct exists in memory yet if l1 was defined just to be a pointer to the allocated memory block...? –  Shiri Jun 28 '13 at 10:02

Your Answer


