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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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