1

I'm writing a program in c in which I need to change the value of a variable from within a function.

I've tried setting the variable globally but it was not recognized withinn the function

So I tried the following: the variable is nobuttons:

readconfig(config2, &nobuttons);

void readconfig(FILE * config, int * buttons) {
    buttons = 5;
}

when I print the value of buttons, it's shown as 0(the value it was initialized to)

what am I doing wrong?

1
  • 1
    Compile with warnings set to their highest level. Commented Jul 2, 2015 at 2:44

1 Answer 1

7

Use *buttons = 5; instead of buttons = 5;

when I print the value of buttons, it's shown as 0(the value it was initialized to)

The value of button is not initialized but as you know, the global variables by default initialized with 0 hence you get 0 when you print it.

buttons=5; means the address of buttons pointer now holding address 5 whereas, *buttons = 5; means the content of buttons pointer is changed with value 5. Remember, the content of whom buttons pointer points will be updated by 5 now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.