Originally Posted by
dayanike
When i need to take a place from memory using with pointer and when i dont need ?
Basically if you want to use the contents of what that pointer points to you will always need to have allocated space.
If you want to iterate through a linked list, you could do:
Code:
int find (struct node * root, int value){
struct node * t; /* you don't need to allocate memory here */
t = root; /* t will point to an already allocated space in memory */
while (t != NULL) {
if (t->data == value)
return 1;
t = t->next;
}
return 0;
}
Pointers are variables which store memory addresses. When you declare them they are storing a random address whose contents are not known. So you initialize them by allocating space or making them point to an already allocated space.
You can also assign them to NULL which is an initialization, but in this case the pointer is still pointing to something now known.
Edit: There's this small tutorial which covers very nice features of pointers, give it a look.