Re: [algogeeks] Re: Doubt regarding Pointers in C......
@Nishaanth, boiling down to how things work everything is pass by value in C (or you can argue for hours as Jammy said) when your function prototype contains a pointer it is simply passed (copied) the address which was contained in the variable(pointer here) which was passed during the function call. use a double pointer as indicated in the above posts. On Thu, Jan 27, 2011 at 9:28 PM, Jammy xujiayiy...@gmail.com wrote: call it by reference. bst *rec = Null; insert(rec,5); void insert(bst *rec,int data){rec-data = data;} Or declare it as void insert(bst **rec,int data){*rec-data = data;} and invoke with insert(rec,5); First one is just syntactic sugar to me although you can argue about the whole pass-by-value/ref/pointer thing for hours. :) On Jan 27, 8:03 am, nishaanth nishaant...@gmail.com wrote: How can we pass pointers by value. Lets say even if it creates a copy of the pointer. The address stored by the pointer is the same. So when we dereference it shouldnt it get updated(as its just a memory address)? On Thu, Jan 27, 2011 at 5:39 PM, ritu ritugarg.c...@gmail.com wrote: Here you are passing record pointer by value. as you call insert(record, 5),stack frame of insert contains a copy of record.after this value is modified by calling program,it should be either returned explicitly to caller program or needs to be passed by reference,so that calling program refers to it always by address. On Jan 27, 4:17 pm, nishaanth nishaant...@gmail.com wrote: Hi guys, I have a small doubt regarding pointers and functions. Consider the following prototype void insert(bst * head, int data); This function creates a node and inserts the data in the node. Lets say i call this function iteratively from main. main(){ bst* record=NULL; insert(record, 5); insert(record, 8); } I see that record is not getting modified. Now is this related to call by value. But since we are passing pointers shouldnt the change get reflected in main. isnt is similar to passing an array? Enlighten me in this regard. I am tired of segfaults :( Regards, S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.comalgogeeks%252bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Man goes to doctor. Says he's depressed. Says life seems harsh and cruel. Says he feels all alone in a threatening world where what lies ahead is vague and uncertain. Doctor says Treatment is simple. Great clown Pagliacci is in town tonight. Go and see him. That should pick you up. Man bursts into tears. Says But, doctor...I am Pagliacci. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Doubt regarding Pointers in C......
Here you are passing record pointer by value. as you call insert(record, 5),stack frame of insert contains a copy of record.after this value is modified by calling program,it should be either returned explicitly to caller program or needs to be passed by reference,so that calling program refers to it always by address. On Jan 27, 4:17 pm, nishaanth nishaant...@gmail.com wrote: Hi guys, I have a small doubt regarding pointers and functions. Consider the following prototype void insert(bst * head, int data); This function creates a node and inserts the data in the node. Lets say i call this function iteratively from main. main(){ bst* record=NULL; insert(record, 5); insert(record, 8); } I see that record is not getting modified. Now is this related to call by value. But since we are passing pointers shouldnt the change get reflected in main. isnt is similar to passing an array? Enlighten me in this regard. I am tired of segfaults :( Regards, S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Doubt regarding Pointers in C......
How can we pass pointers by value. Lets say even if it creates a copy of the pointer. The address stored by the pointer is the same. So when we dereference it shouldnt it get updated(as its just a memory address)? On Thu, Jan 27, 2011 at 5:39 PM, ritu ritugarg.c...@gmail.com wrote: Here you are passing record pointer by value. as you call insert(record, 5),stack frame of insert contains a copy of record.after this value is modified by calling program,it should be either returned explicitly to caller program or needs to be passed by reference,so that calling program refers to it always by address. On Jan 27, 4:17 pm, nishaanth nishaant...@gmail.com wrote: Hi guys, I have a small doubt regarding pointers and functions. Consider the following prototype void insert(bst * head, int data); This function creates a node and inserts the data in the node. Lets say i call this function iteratively from main. main(){ bst* record=NULL; insert(record, 5); insert(record, 8); } I see that record is not getting modified. Now is this related to call by value. But since we are passing pointers shouldnt the change get reflected in main. isnt is similar to passing an array? Enlighten me in this regard. I am tired of segfaults :( Regards, S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Doubt regarding Pointers in C......
call it by reference. bst *rec = Null; insert(rec,5); void insert(bst *rec,int data){rec-data = data;} Or declare it as void insert(bst **rec,int data){*rec-data = data;} and invoke with insert(rec,5); First one is just syntactic sugar to me although you can argue about the whole pass-by-value/ref/pointer thing for hours. :) On Jan 27, 8:03 am, nishaanth nishaant...@gmail.com wrote: How can we pass pointers by value. Lets say even if it creates a copy of the pointer. The address stored by the pointer is the same. So when we dereference it shouldnt it get updated(as its just a memory address)? On Thu, Jan 27, 2011 at 5:39 PM, ritu ritugarg.c...@gmail.com wrote: Here you are passing record pointer by value. as you call insert(record, 5),stack frame of insert contains a copy of record.after this value is modified by calling program,it should be either returned explicitly to caller program or needs to be passed by reference,so that calling program refers to it always by address. On Jan 27, 4:17 pm, nishaanth nishaant...@gmail.com wrote: Hi guys, I have a small doubt regarding pointers and functions. Consider the following prototype void insert(bst * head, int data); This function creates a node and inserts the data in the node. Lets say i call this function iteratively from main. main(){ bst* record=NULL; insert(record, 5); insert(record, 8); } I see that record is not getting modified. Now is this related to call by value. But since we are passing pointers shouldnt the change get reflected in main. isnt is similar to passing an array? Enlighten me in this regard. I am tired of segfaults :( Regards, S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- S.Nishaanth, Computer Science and engineering, IIT Madras. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.