Trouble with Linked List structure program using Command Line inputs

For my next lab test I am required to write a DNS storage and retrival program using pointers and a linked list structure. The program is required to take an ip and uri pair from the command line and add them to the linked list. The Letter A denotes the ip and uri pair which is to be added to the list eg:

A www.google.com 64.125.19.2

The program is also required to find specific ip's and print out the corresponding uri, find specific uri's and print out the corresponding ip, print out the number of ip and uri pairs in the list and delete uri's and it's corresponding ip. I am not concerned in writing the code for these at the moment since I am still trying to figure out how to get my program to read the ip and uri pairs from the command line.

The following is the error messages I obtain when I compile my program. I've included in my code what the error message refers to:

1
2
test3.cpp:182: error: incompatible types in assignment of ‘char*’ to ‘char [50]’
test3.cpp:183: error: incompatible types in assignment of ‘char*’ to ‘char [50]’


The following is the code I have written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>

using namespace std;

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

dns *start_list = NULL;//points to start of linked list
dns *work_ptr;//points to items along the list

int main(int argc, char *argv[])
{
	if (argc == 1)//if there are no values in the command line
     	{ 	
	cout << "student_ID, student_email, My_Name"<< endl ;
       	return(0) ;
     	}
	
    	for(int i=1; i<argc; i++)
    	{
       		if (!strcmp(argv[i],"A"))//finds command line inputs that equal A to obtain the ip address and uri
       		{
		work_ptr = start_list;//saves old start of linked list
		start_list = new dns;//creates a new item
                  //The following is refered to as Line 182 in the error code
            	start_list -> ip = argv[i+1];//the ip address is the first value after A
                  //The following is refered to as Line 183 in the error code
            	start_list -> uri = argv[i+2];//the uri is the second value after A

		//I've only included the next two lines to see if the data is being put into the nodes correctly
		cout << start_list -> ip << endl; 
		cout << start_list -> uri << endl;
		start_list -> next = work_ptr;//the next item points to the previous start of the linked list
        	}        
    	}


   return(0);
}    


I should also tell you that we are not allowed to use the string.h library and that the code must not used Classes to create the linked list.
If anyone could tell me what the problem with my code is I would appreciate it. Also, if I've written any part of the linked list in the incorrect manner I would appreciate it as I've only begun to learn about linked lists and I don't think that I've fully grasp the concept.
Last edited on
You have to copy C strings -- they cannot be assigned with operator=.
Thanks PanGalactic

Now I'm having a problem with finding a specific ip and printing out the corresponding uri. The letter I denotes the ip address to search for in the link list in order to print out it's corresponding uri. The following is the code I have created to do this, which is inserted in the for loop of the code in my opening post. The program will compile but when you run the program and include the letter I followed by the ip in the command line inputs I always get a Segmentation Error message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char ip_target[50];
...
...
...
if(!strcmp(argv[i],"I"))//finds command line inputs that equals I which should be an ip address
{
strcpy(ip_target,argv[i+1]);//copies the value after I to ip_target
work_ptr = start_list;
while( work_ptr != NULL)
{
	if (!strcmp(ip_target, work_ptr->ip))//searches the link list for a matching ip address
        	{
	cout<< work_ptr->uri << endl;//if a match is found the corresponding uri is printed out
	return 0;
        	}
	work_ptr = work_ptr->next;
}
cout<< "nil"<< endl;//if no match is found the output will be nil
}
Topic archived. No new replies allowed.