Thread: Modifying variable from external function

  1. #1
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6

    Question Modifying variable from external function[SOLVED]

    Hi. I'm sure this has been asked before, but I couldn't find an answer, so I'll just ask again:

    I have two separate modules:

    Code:
    mod1.c
    
    f2(int *value) {
    g1(value);
    }
    
    f1() {
    int value;
    f2(&value);
    }
    
    mod2.c
    
    g1(int *value) {
    if(something)  *value--;
    }
    If I printf *value in g1, it decrements correctly. However, when g1 returns to f1, value remains unchanged.

    I'm just not seeing it at the moment what I'm missing, so I'm about to 'give up' and just use "extern int value;" in mod2, its just that I'd like to get this working by 'reference'.

    Any help much appreciated
    Last edited by haifisch; 05-11-2010 at 06:02 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would probably help to do
    Code:
    void g1(int *value) {
         (*value)--;
    }
    Assuming you mean to decrement the value pointed at and not the pointer itself.

  3. #3
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6
    Oh my god

    I'm absolutely sure I tried that at some point...

    Well thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM