C: Rebinding POINTERS in functions

Programming HOW TOs and in-depth guides for programmers of all levels. Programming is an essential skill for hackers, so start learning today!

C: Rebinding POINTERS in functions

Postby TheMuFFenMann » Wed Mar 07, 2007 2:14 am

Hey all. Recently i've run across a few problems with rebinding (changing) POINTERS in functions in C while sorting strings. What follows is an attempt to clarify some possible confusion with passing by reference in C for those somewhat new to the language. Now, there's a difference in changing the variable that the pointer points to and changing what the pointer points to. Got that? For example, look at this:
Code: Select all
#include <stdio.h> void ch_var(int *ptr); int main() { int num = 5; printf("Before the change: %d\n", num); ch_var(&num); printf("The variable num is equal to: %d\n", num); return 0; } void ch_var(int *ptr) { *ptr = 10; }
The above code is simple. It just switches the value in the variable pointed to by the pointer. But in order to change the address that the pointer points to, inside a function other than main, you have to pass the pointer itself by reference. Take the following as an example:
Code: Select all
#include <stdio.h> void ch_ptr(int *ptr); int main() { int n_one = 10; int *n_ptr; n_ptr = &n_one; ch_ptr(n_ptr); if(n_ptr == NULL) printf("n_ptr has been set to NULL.\n"); else printf("n_ptr is still: %d\n", *n_ptr); return 0; } void ch_ptr(int *ptr) { ptr = NULL; }
The above still does not rebind the pointer because it's not passed by reference. Keep in mind that everything in C is passed by value by default, including pointers. To change that, we do this:
Code: Select all
#include <stdio.h> void ch_ptr(int **ptr); int main() { int n_one = 10; int *n_ptr; n_ptr = &n_one; ch_ptr(&n_ptr); if(n_ptr == NULL) printf("n_ptr has been set to NULL.\n"); else printf("n_ptr is still: %d\n", *n_ptr); return 0; } void ch_ptr(int **ptr) { *ptr = NULL; }
First, we change the prototype and the definition of 'ch_ptr' to take a pointer to pointer to int. And when calling the function in main(), we pass the address of the pointer variable itself. This allows pass the pointer by reference and change it in a function. To change it's value (address pointed to), we do this (but note that NULL is not a valid address to point to in C, it's just used for an example of modifying the ptr):
Code: Select all
*ptr = NULL;
Why do we use an asterisk on the pointer you ask? Well, in 'ch_ptr', you're using two levels of indirection. So, To modify it, we dereference it to get to the pointer since when the function is entered, 'ptr' contains the address of the pointer that points to the address of the variable! (confused yet?). To verify the change, when back in main we test it against null, and find that in fact it is changed! That's all folks.

Thanks for reading. Happy Hacking =]
User avatar
TheMuFFenMann
Hacker in Training
 
Posts: 65
Joined: Sun Jul 23, 2006 12:04 am

Postby Nullset » Wed Mar 07, 2007 6:58 pm

you should post this in the tutorials submission section.
I am a bit confused though. :p
The things are easy to do in life are also easy not to do.
http://boundedbyfreedom.blogspot.in/
User avatar
Nullset
htd0rg lieutenant
 
Posts: 440
Joined: Sun Apr 02, 2006 5:47 am

Postby IceDane » Wed Mar 07, 2007 8:58 pm

Yeah, good work there.

Best exercise one can do is explain something to someone else(Or write an explanation for everyone to see) to achieve a better understanding of it.
User avatar
IceDane
Because I Can
 
Posts: 2652
Joined: Wed May 12, 2004 9:25 am

Thanks

Postby TheMuFFenMann » Thu Mar 08, 2007 2:10 am

Thanks for the positive feedback. I will definitely consider writing some more tutorials =]. I'm planning on making an in-depth discussion of handling arrays, multidimensional arrays and pointers properly in C since i know some people out there are somewhat confused by all the ways they can be used.
User avatar
TheMuFFenMann
Hacker in Training
 
Posts: 65
Joined: Sun Jul 23, 2006 12:04 am

Postby NoUse » Thu Mar 08, 2007 6:48 am

Moved to tutorials.

good tut.. pointers can be a little confusing at first, but once you mess around with them a bit, they become your best friend.
And I will strike down upon thee with great vengeance and furious anger
those who would attempt to poison and destroy my brothers.
And you will know my name is the Lord
when I lay my vengeance upon thee.
User avatar
NoUse
time traveller
 
Posts: 2624
Joined: Thu Aug 28, 2003 10:46 pm
Location: /pr0n/fat

Postby IceDane » Thu Mar 08, 2007 3:58 pm

Moved to tutorials.

good tut.. pointers can be a little confusing at first, but once you mess around with them a bit, they become your best friend.
Yes, pointers are one of the main things that make C such a good language.
User avatar
IceDane
Because I Can
 
Posts: 2652
Joined: Wed May 12, 2004 9:25 am

Re: C: Rebinding POINTERS in functions

Postby module0000 » Fri Nov 06, 2009 3:56 am

Fixed that memory leak for you. Statically declared pointers/variables is a nono, more so in kernel-space.
Code: Select all
int main() { int *n_ptr = malloc(sizeof(int)); *n_ptr = 10; ch_ptr(&n_ptr); if(n_ptr == NULL) printf("n_ptr has been set to NULL.\n"); else printf("addressof n_ptr is still: %x\n", n_ptr); free(n_ptr); return 0; } void ch_ptr(int **ptr) { *ptr = NULL; }
module0000
n00b
 
Posts: 13
Joined: Fri Nov 06, 2009 3:42 am

Re: C: Rebinding POINTERS in functions

Postby Smiley » Thu Nov 12, 2009 7:32 am

It's been a while since I coded in C but the integer that he referenced had already been allocated memory. His code looks fine to me.
Smiley
Sargeant at Arms
 
Posts: 238
Joined: Tue Sep 07, 2004 12:29 pm

Re: C: Rebinding POINTERS in functions

Postby IceDane » Thu Nov 12, 2009 11:21 am

Fixed that memory leak for you. Statically declared pointers/variables is a nono, more so in kernel-space.
Code: Select all
int main() { int *n_ptr = malloc(sizeof(int)); *n_ptr = 10; ch_ptr(&n_ptr); if(n_ptr == NULL) printf("n_ptr has been set to NULL.\n"); else printf("addressof n_ptr is still: %x\n", n_ptr); free(n_ptr); return 0; } void ch_ptr(int **ptr) { *ptr = NULL; }
"statically declared variables" - Do you have any idea what you are saying? Sounds like you just slapped the "more so in kernel space" on the end to sound smarter, same with the rest of the crap you wrote.
User avatar
IceDane
Because I Can
 
Posts: 2652
Joined: Wed May 12, 2004 9:25 am

Re: C: Rebinding POINTERS in functions

Postby clueless » Thu Nov 12, 2009 12:07 pm

Just to make this clear in case anyone reads the pointer tutorial later:
The variable he referenced through a pointer was allocated on the stack (local variable), therefore there was no memory leak in the first place as Smiley noted.
Referencing static or local variables with a pointer is not a problem at all as long as you don't reference a variable that has been destroyed.

The code module0000 posted is not faulty code though, but perhaps a bit complex for someone who isn't familiar with pointers. And those are being addressed here in the tutorial.
clueless
Hacker in Training
 
Posts: 70
Joined: Thu Apr 16, 2009 6:26 pm


Return to ā€œ%sā€ Programming / Scripting Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests

cron