开发者

Problem with linked list/pointers

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];

       开发者_JS百科 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.


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;
}


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.


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);

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜