1. ## A question about the polynomial---impelemented with list

Hi all.I wrote a program to impelement the add operation of polynomial with list.For example,I wanna do the add operation on
x^2+3x^4+6x^5 AND x-5x^4+x^5-4x^6
I creat two list,the input should be:
1 2
3 4
6 5
EOF
1 1
-5 4
1 5
-4 6
EOF
my question is at Line 137~140.I haven't initialize the local variable P,however,It has a value!Why?I think it's the key to fix my program.
Code:
```#include<stdio.h>
#include<malloc.h>

typedef struct Polynomial{
int coe;
int order;
struct Polynomial *next;
}Poly;

int main(){

//printf("---------------------\n");
//printf("---------------------\n");
printf("---------------------\n");
return 0;
}

int coe,order;

printf("input the coe and order:\n");
while(scanf("%d %d",&coe,&order) == 2){
//The algorithm will be right only if
//you enter the unique order in order.
Poly *newnode = (Poly *)malloc(sizeof(Poly));
newnode->coe = coe;
newnode->order = order;
newnode->next = NULL;
p->next = newnode;
p = newnode;
printf("input the coe and order:\n");
}
return 1;
}

int flag = 0;
while(pre){
if(pre->order < py->order){
aft = pre;
pre = pre->next;
continue;
}
else if(pre->order > py->order){
aft->next = py;
py->next = pre;
flag = 1;
break;
}
else{
if(pre->coe+py->coe){
pre->coe += py->coe;
flag = 1;
break;
}
else{
Poly p = *py;
flag = 1;
break;
}
}
}
if(!flag){
aft->next = py;
py->next = NULL;
}
}

while(pre){
if(pre->coe == py->coe &&\
pre->order == py->order){
aft->next = pre->next;
//break;
//ignore break in case of more than one matched
}
aft = pre;
pre = pre->next;
}

}

Poly *pre = h1->next,*aft = h1;
Poly *p2 = h2->next;

while(p2 && pre){
if(pre->order < p2->order){
aft = pre;pre = pre->next;
continue;
}
else if(pre->order > p2->order){
Poly p = *p2;

insert_node(h1,&p);
p2 = p2->next;
continue;
}
else{
if((pre->coe+p2->coe) == 0){
aft = pre;pre = pre->next;
del_node(h1,aft);
p2 = p2->next;
}
else{
pre->coe += p2->coe;
aft = pre;pre = pre->next;
p2 = p2->next;
}
}
}

if(pre == NULL){
while(p2){
print_list(h1);
Poly p;//p = *p2;
printf("&&%d %d\n",p.coe,p.order);
print_list(h1);
insert_node(h1,&p);
p2 = p2->next;
}
}
}

while(p){
printf("%+dx^%d",p->coe,p->order);
p = p->next;
}
printf("\n");
}

while(p != NULL){
printf("%d %d\n",p->coe,p->order);
p = p->next;
}
}```

2. > I haven't initialize the local variable P,however,It has a value!Why?

Well it has to have some value. Since you haven't initialized it with a value, the initial value is going to be whatever value that is at the memory location that the variable is bound to.

3. Originally Posted by Memloop
> I haven't initialize the local variable P,however,It has a value!Why?

Well it has to have some value. Since you haven't initialized it with a value, the initial value is going to be whatever value that is at the memory location that the variable is bound to.
I understand.However,if I change the p's value,the value of the list will change,I don't know why