Thread: Can't change variable from another function...

  1. #1
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16

    Can't change variable from another function...

    I have a function foo()

    foo has a local variable var

    I want to change var from inside another function

    Thus, I figured I would do a call to the other function with &var and then change var by dereferencing it and setting it to its new value.

    Apparently this doesn't work... or I am doing something wrong.

    Here's what I have (in a nutshell):

    Code:
    foo () {
    unsigned int var = k;
    printf("var before: %d\n", var);
    bar(&k);
    printf("var after: %d\n", var);
    }
    
    bar(unsigned int* to_change) {
    *to_change = z;
    }
    =====
    [$] ./code

    var before: k
    var after: k
    =====

    Why is var not z!?!? And how do I get it to be z??

    Edit: I just packed them in a malloc'ed array and changed it that way. Still curious as to how this is done though. I recall something like "Handles" but I couldn't find anything useful in Google.

    Edit #2: People shouldn't be allowed to post at 5am... ugh.
    Last edited by Ryan.; 03-10-2011 at 07:08 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might actually want to actually pass var to bar.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    unsigned int var = k;
    printf("var before: %d\n", var);
    bar(&k);
    printf("var after: %d\n", var);

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by CommonTater View Post
    You might actually want to actually pass var to bar.
    He's trying to pass by reference - seems that's the whole point. Passing var to bar won't work (in fact it probably generates a Warning)

    Has the OP edited the code? (I hate that)
    What he wrote should work, ignoring the fact that it's an un-compilable snippet (hate that too!)

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    He's trying to pass by reference - seems that's the whole point. Passing var to bar won't work (in fact it probably generates a Warning)
    In his code k is a magic variable... undeclared and used without initialization, which he passes to the bar() function.

    If he wants to change the content of var from bar he needs to actually pass the address of var into the function, not some mystery meat variable from noplace.


    Code:
    void foo (void) 
       {
         unsigned int var = 09;
         printf("var before: %d\n", var);
         bar( &var );
         printf("var after: %d\n", var);
    }
    
    void bar(unsigned int* to_change) 
      {
         *to_change = 31;
    }

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    bar(unsigned int* to_change) {
    *(to_change) = z;
    }
    Try this....

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by CommonTater View Post
    In his code k is a magic variable...
    Wow, the OP's code is so unfinished I hadn't even noticed he was passing &k instead of &var. Silly me.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Mr.777 View Post
    Code:
    bar(unsigned int* to_change) {
    *(to_change) = z;
    }
    Try this....
    That does absolutely nothing different from what the OP already has. It's just some useless extra parentheses.

  9. #9
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16
    Wow guys, that was pseudocode -- not actual code (I thought this was clear when I said "In a nutshell" and considering it made no sense and would never compile as actual code.) I didn't want to post the actual code because it is too complicated for the problem I *was* having.

    Thanks for the help, though. I'll try to never post problems I am having at 3am+ ever again.

    For the hell of it, here's the actual code I was struggling with last night (this morning):

    Code:
    void big_construct(char big[], char* str)
    {
        unsigned int l, c, s = 0, b = 3;
    
        if (str && (l = strlen(str)))
        {
            big_begin(str, big);
            
            while (1)
            {                 
                if ((c = chars_left(l, s)) >= 2)
                {
                    big_insert(str, big, &s, &b, c = (c >= 3) ? 2 : 1);
                    
                    b += 3*c;
                    s += c;
                    
                    continue;
                }
    
                else
                {
                    big_finish(str, big, s, b);
                    break;
                }
            }
        }
        else *big = NULL;
    }
    
    static void big_insert(char* str, char big[], unsigned int* s, unsigned int* b, unsigned int c)
    {
        while (c--)
        {
            if (!big_contains(*(str + *s), *(str + *s + 1), big))
            {
                big[*b++] = *(str + *s++);
                big[*b++] = *(str + *s);
                big[*b++] = ':';
            }
    
            else
            {
                *s += 2;
                break;
            }
        }
    }
    Last edited by Ryan.; 03-10-2011 at 09:29 PM.

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Really smart??
    Huh... We tried this as a pseudocode... Anyways, good luck if you did it yourself.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change function
    By cody.carter in forum C Programming
    Replies: 19
    Last Post: 05-17-2010, 11:19 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

Tags for this Thread