- #1
cshum00
- 215
- 0
1. I have this code
2. What it does is to
creates a queue called "one" and inserts a value of "2" into it.
Then it prints the value.
The problem is that i have this queue pointer called "tail" which points at the tail of the queue. So when i create the queue (using create_queue function), it obviously will point at the first object. After the program leaves the create_queue functions and goes back to the main function; why the address which the tail pointer is pointing at is not right.
Why after leaving the create_queue function, the tail value changes?
Code:
typedef struct linkedlist {struct linkedlist *next; key_t value;} queue;
const queue *tail;
queue *create_queue(key_t input) {
queue temp;
temp.next = NULL;
temp.value = input;
tail = &temp;
printf("%d\n", tail->value);
return &temp;
}
int main() {
queue *one;
one = create_queue(2);
printf("%d\n", one->value);
printf("%d\n", tail->value);
return(0);
}
2. What it does is to
creates a queue called "one" and inserts a value of "2" into it.
Then it prints the value.
The problem is that i have this queue pointer called "tail" which points at the tail of the queue. So when i create the queue (using create_queue function), it obviously will point at the first object. After the program leaves the create_queue functions and goes back to the main function; why the address which the tail pointer is pointing at is not right.
Why after leaving the create_queue function, the tail value changes?