user3656667
user3656667

Reputation: 13

Passing parameter cannot be edited

Why passing parameters couldn't be changed. I have some function

void read_errorlog (int success, char *names, int empty) {

that is called from another, where

  const char *names[2] = {"errorlog.txt","errorlog1.txt"}
  int empty = 0;
  int counter = 0;
  int success = 0;

all variables are initialized. Inside of void read_errorlog neither int success, not int empty change there values. I do it like empty = 0; i was calling the function with read_errorlog (one, two, three). I got now, that i have to pass addresses of int variables. Thank you.

Upvotes: 0

Views: 46

Answers (1)

Cantfindname
Cantfindname

Reputation: 2148

This is the difference of call by value (what you are doing) and "call by reference" (what you should do).

When you call the function read_errorlog, the arguments that are passed are in reality copies of the variables that you give as arguments. So, when you do empty=0 inside the function, you change the value of the copy, but not of the original variable.

For "calling by reference", you need to pass a pointer to empty and a pointer to success instead of their values. This can be done as follows:

void read_errorlog (int* success, char *names, int* empty) {
    *empty = 0;
    *success = 1;
}

read_errorlog(&success, names, &empty);

Upvotes: 1

Related Questions