why float value in structute can be used for linkedlist ???

This is a discussion on why float value in structute can be used for linkedlist ??? within the C Programming forums, part of the General Programming Boards category; if you are taking a float variable in a structure, why you can't use it in linked-list? why you need ...

  1. #1
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    Question why float value in structute can be used for linkedlist ???

    if you are taking a float variable in a structure, why you can't use it in linked-list?
    why you need to take a floating value first in a temporary variable and then assigning it to float variable of structure? some people write a function to avoid this, what is that and what is reason for that?
    Last edited by Salem; 10-07-2010 at 11:34 AM. Reason: Font abuse--

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    None at all.

    A float in a struct is no different to a normal float variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Quote Originally Posted by gaurav9991 View Post
    if you are taking a float variable in a structure, why you can't use it in linked-list?
    why you need to take a floating value first in a temporary variable and then assigning it to float variable of structure? some people write a function to avoid this, what is that and what is reason for that?
    Yes you can. There is no such limitation, no reason to think there is a problem, and hence nothing to avoid.
    Whatever you're referring to, you've misunderstood or been mislead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by Salem View Post
    None at all.

    A float in a struct is no different to a normal float variable.
    I'm not talking about 'float in structure', it's 'float in structure which is used in linked-list'.

    if you are accepting a floating value in a structured float variable i.e. using 'scanf', of course in dynamically allocated space (for linked-list), the program terminates unusually.

    I'm using Turbo c++ editor and haven't tried on other editors, so, is it a problem of editor ?

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    May want to get yourself another IDE. Floats can be used in linked list as the data.

  6. #6
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by Syscal View Post
    May want to get yourself another IDE. Floats can be used in linked list as the data.

    I'm sorry, but you are not getting my problem.

    First thing is I'm using turbo c++ editor

    I know that float can be used in linked-list , i said that if i am accepting a floating value using scanf function in a float variable declared in structure in a dynamically allocated space(such as linked-list), the problem occurs

    thanks for the reply

  7. #7
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Well have you got any code with an example of your problem?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Well you´re just going to have to show your code, because you´re not making a lot of sense.

    Code:
    struct node {
       float next;
    };
    doesn´t make any sense, if you´re expecting next to get you to the next node of the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I would guess that it probably has something to do with accessing structure element using '.' vs. '->', or putting the '&' to denote address-of, which the OP is doing wrong.

  10. #10
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    working if "marks" is an integer

    here is the code which works if "marks" is declared as an "int" and using format specifier "%d" instead of "%f"
    tested on turbo c++ editor only

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<alloc.h>
    
    typedef struct LinkedList
    {
       int roll;
       float marks;
       char name[20];
    
       struct LinkedList *next;
    }Node;
    
    Node* CreateNode()
    {
    	Node *temp;
    
    	temp=(Node*)malloc( sizeof(Node) );
    
    	printf("\n\t\tEnter roll no : ");
    	scanf("%d",&temp->roll);
    	printf("\n\t\tEnter name : ");
    	flushall();
    	gets(temp->name);
    	printf("\n\t\tEnter Marks : ");
    	scanf("%f",&temp->marks);
    
    	temp->next=NULL;
    
    	return temp;
    }
    Node* AddNode(Node *head)
    {
    	Node *newnode,*temp;
    	temp=head;
    
    	newnode=CreateNode();
    	if(head==NULL)
    	{
    		head=newnode;
    	}
    	else
    	{
    		while(temp->next!=NULL)
    			temp=temp->next;
    
    		temp->next=newnode;
    	}
    
    	return head;
    }
    void Display(Node *head)
    {
    	Node *temp;
    	temp=head;
    
    	printf("\n\nRoll no    Name \t\tMarks\n");
    
    	while(temp!=NULL)
    	{
    		 printf("\n %d\t   ",temp->roll);
    
    		 cputs(temp->name);
    
    		 printf("\t  %f ",temp->marks);
    
    		 temp=temp->next;
    	}
    	printf("\n\n");
    }
    
    void Displayrev(Node *temp)
    {
    	if(temp->next!=NULL)
    		Displayrev(temp->next);
    
    	 printf("\n %d\t   ",temp->roll);
    
    	 cputs(temp->name);
    
    	 printf("\t  %f ",temp->marks);
    }
    
    Node* Delete(Node *head)
    {
    	int option,r;
    	Node *temp,*previous;
    
    	temp=head;
    
    	printf("\n\t\tEnter option. \n\t\t1. Delete first record\n\t\t");
    	printf("Delete a particular record.\n\t\t3.Delete last record\n");
    	scanf("%d",&option);
    
    	switch(option)
    	{
    		case 1:
    				head=head->next;
    				free(temp);
    				break;
    		case 2:
    				printf("\nEnter roll no. to delete the record: ");
    				scanf("%d",&r);
    
    				while(r!=temp->roll)
    				{
    					previous=temp;
    					temp=temp->next;
    				}
    				previous->next=temp->next;
    				free(temp);
    				break;
    
    		case 3:
    			   while(temp->next!=NULL)
    			   {
    				 previous=temp;
    				 temp=temp->next;
    			   }
    			   previous->next=NULL;
    			   free(temp);
    			   break;
    	}
    
    	return head;
    }
    Node* Insert(Node *head)
    {
      int option,pos,loc=1;
      Node *temp,*newnode;
      temp=head;
    
      newnode=CreateNode();
    
      printf("\n\t\tEnter option. \n\t\t1. Insert at begining.\n\t\t2. Insert ");
      printf("at particular position.\n\t\t3.Insert at end\n");
      scanf("%d",&option);
    
      switch(option)
      {
    	case 1:
    			newnode->next=temp;
    			head=newnode;
    			break;
    	case 2:
    			printf("\nEnter Position at which you want to insert record \n");
    			scanf("%d",&pos);
    
    			while(loc!=pos-1)
    			{
    				  temp=temp->next;
    				  loc++;
    			}
    
    			newnode->next=temp->next;
    			temp->next=newnode;
    			break;
    
    	case 3:
    			while(temp->next!=NULL)
    				temp=temp->next;
    
    			temp->next=newnode;
    			break;
    	}
    
    	return head;
    }
    
    void Search(Node *head)
    {
    	Node *move;
    	int r,flag=0;
    	move=head;
    
    	if(head==NULL)
    		printf("\nHmmmmm .... Linked List is not yet created!!!!");
    	else
    	{
    		printf("\nEnter Id\n");
    		scanf("%d",&r);
    		while(move!=NULL)
    		{
    			if(move->roll==r)
    			{
    					printf("\nRecord found\n");
    					flag=1;
    
    					printf("\n\nRoll no    Name \t\tMarks\n");
    
    					printf("\n %d\t   ",move->roll);
    					cputs(move->name);
    					printf("\t  %f ",move->marks);
    			 }
    			   move = move->next;
    		}
    		if(move==NULL && flag==0)
    			printf("\nRecord not found !!!\n");
    	 }
    }
    
    Node* Modify(Node *head)
    {
    	Node *move;
    	int r,flag=0;
    	move=head;
    
    	if(head==NULL)
    		printf("\n\nRecord is not yet created !!!");
    	else
    	{
    		printf("\nEnter Roll no : ");
    		scanf("%d",&r);
    		while(move!=NULL)
    		{
    			if(move->roll==r)
    			{
    				printf("\n\t\tEnter roll no : ");
    				scanf("%d",&move->roll);
    				printf("\n\t\tEnter name : ");
    				flushall();
    				gets(move->name);
    
    				printf("\n\t\tEnter Marks : ");
    
    				scanf("%f",&move->marks);
    
    				flag=1;
    			 }
    			   move = move->next;
    		}
    		if(move==NULL && flag==0)
    			printf("\nNode not found !!!\n");
    	 }
    	 return head;
    }
    
    void Freeall(Node *head)
    {
    	Node *temp=head;
    
    	while(temp!=NULL)
    	{
    	   head=head->next;
    	   temp->next=NULL;
    	   free(temp);
    	   temp=head;
    	}
    }
    
    void main()
    {
    	int option,roll,ch;
    	Node *head=NULL;
    	clrscr();
    
    	do
    	{
    	printf("\n\n**************************** STUDENT DATABASE ****************************");
    	printf("  \n\n\t\t\t 1.Add Record\n\t\t\t 2.Delete Record \n\t\t\t 3.Insert Record \
    		  \n\t\t\t 4.Display Record \n\t\t\t 5.Search Record \n\t\t\t 6.Modify  \
    		  \n\t\t\t 7.Freeall \n\t\t\t 8.Exit \n\n\t\t\t ");
    	scanf("%d",&option);
    	switch(option)
    	{
    		case 1:
    				head=AddNode(head);
    				break;
    		case 2:
    				head=Delete(head);
    				break;
    		case 3:
    				head=Insert(head);
    				break;
    		case 4:
    				if(head==NULL)
    				{
    					printf("\n\nRecord is Empty !!!");
    					break;
    				}
    
    				printf("\n\n1:Display serially  \n\n2:Display Reverse \n\n");
    				scanf("%d",&ch);
    
    				printf("\n\nRoll no    Name \t\tMarks\n");
    
    				switch(ch)
    				{
    					case 1:
    						Display(head);
    						break;
    
    					case 2:
    						Displayrev(head);
    						break;
    
    					default:
    						printf("\n\nInvalid Choice !!!");
    
    				 }
    				break;
    
    
    		case 5:
    				Search(head);
    				break;
    		case 6:
    				head=Modify(head);
    				break;
    		case 7:
    				Freeall(head);
    				head=NULL;
    				break;
    	}
    
    	}while(option!=8);
    
    	getch();
    }

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I wonder if scanf first assumes double??? If you used a temporary variable first, then assigned it to the structure element, you'd be doing an implicit conversion from double to float somewhere along the line as needed. However, inputting directly to the structure element from scanf() might overwrite some adjacent data if the default is double. Still sounds like a compiler bug.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Yeah that would be a compiler bug. Have you tried %e or %g instead?

    Don't use Turbo C, it's horrifically broken and outdatated. Visual Studio Express 2008 and Code Blocks are two good alternatives.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    In his book Expert C Programming, Peter van der Linden mentions a similar type of problem that is the result of Borland's compilers not quite being smart enough to know when to provide floating point support. His suggested workaround is to add the following function:
    Code:
    static void forcefloat(float *p)
    { float f = *p; forcefloat(&f); }
    Never call this, of course.

    It's worth a try, I guess, but as has been suggested, stop using Turbo C. There's no need, unless your school/instructor is so backward that it's required.

  14. #14
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    thanks to all of you people for your suggestions, it will definitely help me

    thanks a lot

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Lemme guess, you saw an error message like "floating point not linked" and never bothered to mention it

    Post CODE AND ERROR MESSAGES. We´re not here to waste posts and days on every single noob to try and "squeeze blood out of a stone".

    > gets(temp->name);
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    > void main()
    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21