c - Pass by value-doesn't execute as I want it -


this question has answer here:

i not understand concept of pass value in c. here function:

void add(int x){    x = x+1; } 

and when call function:

int y=3;   add(y); 

but when compile, still 3. i've been told has pass-by-value still not understand? can explain why?

pass value creates copy of argument. copy changed in function

void add(int x){    x = x+1; } 

thus change make made copy , not variable in main scope (that expecting see changed).

if want change variable within function passing parameter cannot pass value. change function pass pointer this

void add(int* x){    *x = *x + 1; } 

and pass address of integer function this

int y=3; add(&y); 

within main()

the pointer still passed value copy of pointer being acted on, doesn't matter not changing pointer itself, changing value of variable points to.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -