0

I'm currently having an issue with linked list and pointers in C. The problem I'm having is with adding data to a linked list. Currently I have:

struct str_pair{ 
char ip  [50] ;
char uri [50] ;
struct str_pair *next ;
};

struct str_pair *it ;

struct str_pair *header = NULL; // Start of linked list
struct str_pair *ptr; // Moves along the list
struct str_pair *ptr2; // Another pointer
struct str_pair *ptr3;

void addData(char *addURI, char *addIP){

    struct str_pair *tmp, *tmp2;

    tmp = (str_pair*)malloc(sizeof(str_pair)); // Create new space in str_pair
    strncpy(tmp->uri, addURI, 49);
    strncpy(tmp->ip, addIP, 49);
    tmp->next = NULL;

    if (header == NULL) { header = tmp; }
    else
    {
        tmp2 = header;
        while (tmp2->next != NULL) { tmp2 = tmp2->next; }
        tmp2->next = tmp;
    }
}

What i'm trying to do is pass a URL and an IP address through the parameters in which it should add those values into the linked list.

Here is code that calls this function:

int main(int argc, char *argv[])
{
    int incrItems=0;
    int j;

    header = NULL;

    for(j = 1; j < argc; j++)
    {
        char ch=argv[j][0];

        switch(ch)
        {
        case 'A' :
            {
                char *newURI = argv[j+1];
                char *newIP = argv[j+2];
                incrItems++;
                addData(newURI,newIP);
                j=j+2;
                break;
            }

*Snipped the rest as its unnecessary*

The problem I'm having is that the passed arguments are not being added to the linked list. No error is shown when compiling.

5
  • What problem do you have exactly?
    – Mat
    Sep 18, 2011 at 14:05
  • You actually forgot to mention your problem!
    – Alok Save
    Sep 18, 2011 at 14:08
  • The problem is that its not working. The data from passed arguments is not showing in the linked list. No error is shown when compiling the code.
    – George
    Sep 18, 2011 at 14:10
  • 1
    Is this homework? New user writing linked lists from scratch in C in mid-September? Sep 18, 2011 at 14:16
  • @George: "Not working" is not a "problem" description. Make a testcase, narrow down the issue and present a specific question please. This is not debugmycodeplz.com -- it is a site for interesting questions about programming languages. Sep 18, 2011 at 14:17

3 Answers 3

0
for(j = 1; j < argc; j++)
    {
    switch(argv[j][0]) { /* no need to copy */
    case 'A' :
            incrItems++;
            /* assert (j+2 < argc); */
            addData(argv[j+1], argv[j+2]); /* no need to copy */
            j=j+2;
            break;
    case 'B' :
    default:
      ...
            break;
        }
     }

EDIT: Note: the above is not the solution, merely a hint. Another hint:

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

void addData(char *addURI, char *addIP){

    struct str_pair *tmp, **hnd;

    tmp = malloc(sizeof *tmp);
    /* assert (tmp != NULL); */
    strncpy(tmp->uri, addURI, 49); tmp->uri[49] = 0;
    strncpy(tmp->ip, addIP, 49); tmp->ip[49] = 0;
    tmp->next = NULL;

    for (hnd = &header; *hnd; hnd = &(*hnd)->next) {;}
    *hnd = tmp;
}
1
  • Hahaha not too bright :), it's late where I am so I'm taking my time to process your "hints". Your help is appreciated :D.
    – George
    Sep 18, 2011 at 14:37
0

If you're inside a for, why are you incrementing the j variable? Plus, try putting some printf inside the add function to know if the arguments are correct.

0

Although the nice hints provided by wildplasser, but the code works perfectly fine.

int main(int argc, char *argv[])
{
    int incrItems=0;
    int j;

    header = NULL;

    for(j = 1; j < argc; j++)
    {
        char ch=argv[j][0];

        switch(ch)
        {
        case 'A' :
            {
                char *newURI = argv[j+1];
                char *newIP = argv[j+2];
                incrItems++;
                printf(" Adding %s %s\n", newURI, newIP);
                addData(newURI,newIP);
                j=j+2;
                break;
            }
        }
     }
printf(" J at end is %d\n",j);

it = header;
if(it != NULL)
do {
        printf(" %s %s\n",it->ip, it->uri);
        it = it->next;
}while(it != NULL);

    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.