C, pass by value, in Linux

90 Views Asked by At

I have a function as follows:

void foo (int *check){
*check= 9;
printf("*check: %d\n",*check);
//when I print "*check" here, the value changes as 9.
}

This is the main function.

void main () {
int check=5;
foo(&check);
printf("check: %d\n",check);
//when I print "check", gives me 5. 
}

I want to change the value of "check" variable but it does not work. Where am I making mistake? Thank you!

I am using makefile while running it. Edit: malloc is deleted now it gives me Segmentation fault (core dumped) error

4

There are 4 best solutions below

3
Nastor On

You are doing everything just fine, the only thing that is wrong is trying to malloc the variable you passed to the function , since it is already allocated on stack. In order to do what you're trying to do, you should declare the integer as pointer to integer (int *check) outside the function, and avoid using the & character when calling the function with the pointer as parameter.

2
PotatoParser On

When you malloc within foo, the dereference now points to the value malloced within the function's scope. This means that the value within the function is changed and not the value outside of the function. To correct this, you don't need to malloc within the function to change the value of check:

void foo (int *check) {
    *check= 9;
}

void main () {
    int check = 5;
    foo(&check); // check is now 9
}
0
Vlad from Moscow On

You passed the variable check to the function foo by reference through a pointer to it

int check=5;
foo(&check);

So dereferencing the pointer you could get a direct access to the variable check and could change it like

*check= 9;

However within the function you reassigned the pointer with a new address of a dynamically allocated memory

check=  malloc(sizeof(int));

So now the pointer check doe not point to the original object passed to the function by reference. As a result this statement

*check= 9;

changes the dynamically allocated object of the type int instead of changing the variable passed to the function by reference.

Edit: malloc is deleted now it gives me Segmentation fault (core dumped) error

It is a bad idea to change such dramatically the code in the question because it will only confuse readers of the question and answers. Neither segmentation fault should occur. It seems the new provided code in the question does not correspond to the actual code that generates a segmentation fault.

Here is a demonstrative program. It compiles and runs successfully.

#include <stdio.h>

void foo ( int *check )
{
    *check = 9;
    printf( "Inside foo check = %d\n", *check );
}

int main(void) 
{
    int check = 5;
    
    printf( "Before foo check = %d\n", check );
    
    foo( &check );
    
    printf( "After  foo check = %d\n", check );

    return 0;
}

The program output is

Before foo check = 5
Inside foo check = 9
After  foo check = 9
0
AudioBubble On

To change a variable through a function you need to pass it by reference, not value. Your title and code do not match.

Here is a version with both kinds of arguments, side by side:

#include <stdio.h>

int foo(int *ref, int val) {
    *ref = 1122;                   // by reference
    val = 1155;                    // by value
    printf("inside:\n%d %d\n", *ref, val);
    return val;                    // otherwise "val=1155" is lost
}
int main(void) {
    int ree = 12;         // "referencee"
    int v = 15;

    printf("main:\n%d\n", foo(&ree, v));    // return value (1155)
    printf("%d %d\n", ree, v);              // 1122 and 15

}

ree is passed as &ree to ref; this address/pointer gets dereferenced inside the function with *ref to change ree's value.