0

I have a linked list whose elements are of type

typedef struct List * News;
struct List
    {
         char * Text;
         News NextN;
    };

In the main function I declared an array of type News as follows

News PTH[50];
for (i = 0; i < 50; i++)
    {
         PTH[i] = (News) malloc(sizeof(struct List));
         PTH[i] -> Text = NULL;
         PTH[i] -> NextN = NULL;
    }

I added new nodes in the beginning of the list with

if ( PTH[i] -> Text == NULL)
    PTH[i] -> Text = msg;
else 
    {
        t -> Text = msg;
        t -> NextN = PTH[i];
        PTH[i] = t;
    }

Where msg is a array of char of length 2000; and and then tried to print the texts apointed by PTH[i] -> Text with

p = PTH[i];
if (p -> Text != NULL)
    {
        printf("%s", p -> Text);
        p = p -> NextN;
    }
while (p != NULL)
    {
        printf("%s", p -> Text);
        p = p -> NextN;
    }
}

This algorithm only add one node. The error is how I define the PTH or is there an error in how I put nodes in the list.

9
  • 1
    We can assume the typedef for News is before the List structure definition, right? Because as written, this won't compile. Further, sizeof(struct News) ? There is no struct News, so that won't compile regardless. Kindly post the real code in compilable form. Commented Nov 3, 2013 at 21:09
  • @WhozCraig, sorry, I edit the errors. They are not in the my program. My program is over 200 lines long and in portuguese, do I post it (The other parts are work fine)? Commented Nov 3, 2013 at 21:17
  • @claptrap each PTH[i] correspond to an news agency, and the nodes are the news, organized by the time of arriving. Commented Nov 3, 2013 at 21:22
  • 1
    @PauloHenrique ok i think i understand now, you want read texts and set them in the vector if there is previously a text you want to add a new node - so its an array of linked lists? otherwise it seems superfluous to have a link between each node when you anyway can just get the next in the array. Commented Nov 3, 2013 at 21:26
  • @claptrap yes. And after, print them in the screen, in order. Commented Nov 3, 2013 at 21:30

2 Answers 2

2

maybe it is something like this you are after:

if ( PTH[i]->Text == NULL )
{
  PTH[i]->Text = msg;
}
else // first create node, then append by first finding the last node
{
  News it = NULL;
  News t = malloc( sizeof(struct List));
  t->Text = msg;
  t->NextN = NULL;

  for ( it = PTH[i]->NextN; it != NULL; it = it->NextN)
  {
    ;
  }
  it->NextN = t;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I corrected that, these errors are not in my original program.
Shouldn't that be for ( it = PTH[i]->NextN; it->NextN != NULL; it = it->NextN) ?
1

Assuming that msg is a buffer you use to receive new data, you have to be careful with this statement:

PTH[i] -> Text = msg;

Since msg is a pointer to char, the assignment will not copy the characters sequence; instead, it will just make PTH[i]->Text point to the same location as msg. This is problematic if you change the contents in msg - the changes are, of course, reflected back in every PTH[i]->Text for which the assignment was made, namely, every node that you ever added. Probably, not quite what you want. This is why it seems like you can only handle one node at a time. They all get the same text, because they all point to the same memory location.

You should use strcpy instead:

strcpy(PTH[i]->Text, msg);

Don't forget to include string.h.

This assumes that PTH[i]->Text is already allocated. You might want to use strncpy if there is a chance that msg exceeds 2000 characters, to avoid buffer overflows.

If you didn't alloc space for PTH[i]->Text, you could allocate exactly strlen(msg)+1 positions for PTH[i]->Text and then use strcpy safely. Or you could use strdup, also declared in string.h, which has exactly this behavior:

PTH[i]->Text = strdup(msg);

4 Comments

can I copy the content of msg to the pointer PTH[i] -> Text ?
@PauloHenrique Did you alloced space for PTH[i]->Text? e.g. PTH[i]->Text = malloc(...)
NO, I did alloced for the PTH only.
Then you can't copy directly before allocating memory for PTH[i]->Text. You can use strdup (string duplicate) to do that for you. I updated my answer, check it. It's as simple as: PTH[i]->Text = strdup(msg);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.