Thread: Problem passing pointers between functions

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Angry Problem passing pointers between functions

    Hey guys, I'm obviously somewhat new to C and am having problems passing pointers between functions in two different .c files.

    Basically, I have this in main.c:

    ...
    printf("Address of jump: %d\n", &jump);
    store(&jump, address, op);
    ...

    and in table.c I have:

    void store(table *tab, int val, char* k)
    {
    printf("Address of received table: %d\n", &tab);
    ...

    jump itself is declared in a global header file. It is a typedef struct.

    When I run the program, I get this:
    Address of jumpTable: 17669248
    Address of received table: 3341436

    Why on Earth are the addresses being pointed to changing when I pass them to different functions? I've been looking around everywhere I can for answers but have not been able to find any. Please help me out!

    * Obviously I've removed most of my program to focus on what (I think) are the relevant bits. If more info is needed, please just let me know.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Because jump is already a pointer and you're passing in IT's address not it's contents...

    Code:
    store(jump,address,op);

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Firstly you print the address of jump, and then print the address of tab, not *tab.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    What I'd like to do is pass the address of jump so the store function can modify it. I tried removing the ampersand in

    Code:
    store(jump,address,op);
    as well as the corresponding splat in

    Code:
    void store(table tab, int val, char* k)
    ...but I still get different address'.

  5. #5
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Remember, in C even pointers are passed by value, so by taking the address of a pointer passed to a function you are actually getting the address of a local copy of that pointer. The solution is to pass a pointer to a pointer.

    Code:
    void reset(int** ptr, int* adr)
    {
    	*ptr = adr;
    }
    
    int main(void)
    {
    	int a = 1024, b = 512, *p = 0;
    	printf("Address of 'a': %x\n", (unsigned)&a);
    	printf("Value of 'a':   %d\n", a);
    	printf("Address of 'b': %x\n", (unsigned)&b);
    	printf("Value of 'b':   %d\n", b);
    	puts("reset: p -> a");
    	reset(&p, &a);
    	printf("Value of 'p':   %x\n", (unsigned)p);
    	printf("Value of '*p':  %d\n", *p);	
    	puts("reset: p -> b");
    	reset(&p, &b);
    	printf("Value of 'p':   %x\n", (unsigned)p);
    	printf("Value of '*p':  %d\n", *p);		
    }
    Last edited by gardhr; 09-17-2011 at 09:46 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Civus View Post
    What I'd like to do is pass the address of jump so the store function can modify it. I tried removing the ampersand in

    Code:
    store(jump,address,op);
    as well as the corresponding splat in

    Code:
    void store(table tab, int val, char* k)
    ...but I still get different address'.
    Nope... you should have laft it in the store function...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Aha, thanks for the help guys. I guess it's just taking me a while to get used to this pass-by-value convention. Thanks again!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Civus View Post
    Aha, thanks for the help guys. I guess it's just taking me a while to get used to this pass-by-value convention. Thanks again!
    Coming from a Turbo Basic and Pascal background I *really hated* pointers when I first started with C ... But once you get the hang of it, it's actually quite simple. Just remember... in C everything is a number and everything passed into a function is a copy of a number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers to Functions question..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 10-18-2008, 03:51 PM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM