-1
void create(struct node *head);                   //declaring functions
void display(struct node *head);     
struct node                                 //creating a struct datatype for nodes        
{
 int coeff;
 int power;
 struct node* next;
};
  struct node* poly1=NULL;               //head pointer for a sample polynomial

  void main()
 {
   int coeff,power,deg,i;
   clrscr();                                              //main function
   printf("\n enter polynomial 1 ");
   create(poly1);
   display(poly1);
   getch();
 }

  void create(struct node *head)
 {
  struct node *newnode,*temp;
  int exp,num,n,i;
  printf("\n enter no. of terms in your expression=");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
   newnode=(struct node*)malloc(sizeof(newnode));
   newnode->next=NULL;
   printf("\n enter power=");
   scanf("%d",&exp);
   newnode->power=exp;
   printf("\n enter coefficient=");
   scanf("%d",&num);
   newnode->coeff=num;
    if(head==NULL)
     head=newnode;
    else
     {
      temp=head;
      while(temp->next!=NULL)
      {
       temp=temp->next;
      }
       temp->next=newnode;
     }
 }
}
 void display(struct node *head)
 {
  struct node *temp;
  temp=head;                                                 
  while(temp->next!=NULL)
  {
   printf("%dx^%d",temp->coeff,temp->power);
   temp=temp->next;
   }
 }

My code compilation shows no errors or warnings but I am not able to print my polynomial function. The display function is printing just 0 x^ 0 or no values at all, I have tried many things but it is not working, can someone point out what should I do to correct it. I am using C language.

SKumar
  • 43
  • 7
  • 3
    No warnings? I get tons of warnings for your code. – klutt Dec 17 '19 at 09:32
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Dec 17 '19 at 10:32

2 Answers2

2

My code compilation shows no errors or warnings

That's very strange. After adding missing includes and commenting out getch and clrscr this is the compiler output and it's without even enabling the (almost mandatory) flags -Wall -Wextra:

$ gcc main.c
main.c:4:20: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
    4 | void create(struct node *head);                   //declaring functions
      |                    ^~~~
main.c:5:21: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
    5 | void display(struct node *head);
      |                     ^~~~
main.c: In function ‘main’:
main.c:19:11: warning: passing argument 1 of ‘create’ from incompatible pointer type [-Wincompatible-pointer-types]
   19 |    create(poly1);
      |           ^~~~~
      |           |
      |           struct node *
main.c:4:26: note: expected ‘struct node *’ but argument is of type ‘struct node *’
    4 | void create(struct node *head);                   //declaring functions
      |             ~~~~~~~~~~~~~^~~~
main.c:20:12: warning: passing argument 1 of ‘display’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |    display(poly1);
      |            ^~~~~
      |            |
      |            struct node *
main.c:5:27: note: expected ‘struct node *’ but argument is of type ‘struct node *’
    5 | void display(struct node *head);
      |              ~~~~~~~~~~~~~^~~~
main.c: At top level:
main.c:24:8: error: conflicting types for ‘create’
   24 |   void create(struct node *head)
      |        ^~~~~~
main.c:4:6: note: previous declaration of ‘create’ was here
    4 | void create(struct node *head);                   //declaring functions
      |      ^~~~~~
main.c:53:7: error: conflicting types for ‘display’
   53 |  void display(struct node *head)
      |       ^~~~~~~
main.c:5:6: note: previous declaration of ‘display’ was here
    5 | void display(struct node *head);

Your compiler seems to be seriously outdated.

There seems to be many issues with your code, but one obvious is this:

newnode=(struct node*)malloc(sizeof(newnode));

The casting is bad practice, but should not cause any actual problems, but the argument to sizeof is wrong. It should be:

newnode=malloc(sizeof(*newnode));
klutt
  • 30,332
  • 17
  • 55
  • 95
1

Here you pass the pointer by value

create(poly1);

to your function

void create(struct node *head)

then you set head

head=newnode;

but are only modifying the local variable head, while you want to modify poly1.

One possibility is to pass a double pointer like this:

 create(&poly1);
 ...
 void create(struct node **head)
 ...
 if (*head == NULL)
     *head = newnode;

Now you really modify the global variable poly1, which is what you want.

Ctx
  • 18,090
  • 24
  • 36
  • 51