Jump to content

Problem With Static List Of Structs In C


swcratis
 Share

Recommended Posts

Hi!!I' trying to make a server/client programm in whitch there are 3 topics in a list and the client must choose one of them and the sever must print it in stdout.Also i'm writing a function that is tying to find the topic that client asked for.The topics are structs with 2 characteristics topicID and topic_name.But i think that im a bit confused with the pointers lists use.Here is the server code:

#include <sys/types.h>	
#include <sys/socket.h>
#include <errno.h>			/*Diaxeirish munhmatwn sfalmatos*/
#include <string.h>
#include <stdio.h>			/* for printf */
#include <stdlib.h>	
#include <unistd.h>		//standard unix functions, like getpid() 
#include <sys/types.h>	  //various type definitions, like pid_t 
#include <signal.h>		   //signal name macros, and the kill() 		
#define LISTENQ 5	/*gia to magethos tis ouras tou server*/
#define true 1
#define false 0


struct topic {
   	   int topicID;
   char topic_name[20];
	struct topic *next;
   };

	struct topic m[3]={
   		 {1,"Computers"},
   		 {2,"culture"},
   		 {3,"Fashion"}
		 };


struct topic *head;
struct topic *topic_ptr;
//t head;
//head=&topic_1;
//topic_1.next=&topic_2;
//topic_2.next=&topic_3;
//topic_3.next=NULL;


findtopic(topic_ptr head) /* Briskei to topic pou epilegei o client analoga me to topicID tou thematos */
		{
		int a,found;
		topic_ptr p;
		topic_ptr=m;
		//p=h;
		found=false;
		printf("Please give the id of the topic to be found:");
		scanf("%d",&a);
		while(p!=NULL && p->topicID!=a) p=p->next;		 //psaxnei ena ena ta themata tis listas
		if(p!=NULL)
		{
		printtopic(p);
		found=true;
		}
		if(!found) printf("\nThe topic is not found\n");
		}	

		printtopic(topic_ptr nd) {
			   topic_ptr=m;
			 printf("The topic is found\n");
			 printf("topicID:%d\ntopic_name:%s\n",nd->topicID,nd->topic_name);		
			 }


int main( int argc, char **argv ) 
{
	topic_ptr=m;
	int option, done;
done=false;
head=NULL;
printf("The options you have are:\n");
printf("1.anagnwsi anakoinwsewn\n");
printf("2.apostoli anakoinwsewn\n");
printf("\nPlease choose an option:");
scanf("%d",&option);		
printf("\n");

switch(option)
{
 			  case 1:findtopic(head);
			  break;
  }
  }																	//Anagnwsi anakoinwsewn apo kapoio topic

 

Can anyone tell how pointers work excactly and if my code for the list is correct??

thanks.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...