-5
#include <stdio.h>
    
int value(int *a);
    
int main(){
    int num = 4;
    value(&num);
    printf("value of number is = %d", num);
    return 0;
}

int value(int *a){
    int c = (*a)*10;
    return c;
}

In this code, I transfer the address in function but it does not change, Why?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 6
    You are returning from `value` function and not assigning anywhere? `value(&num);` – Suraj Rao Feb 07 '22 at 05:16
  • 3
    And the function parameter to `value` does not need to be a pointer since you are not updating it. – kaylum Feb 07 '22 at 05:20
  • 2
    Alternately, since you are passing the pointer, you can directly change the value under the pointer; but `c` is a local variable, and will not affect the content under `a`. Changing `int c = (*a) * 10` into `*a = *a * 10` (or equivalently, `*a *= 10`) will produce the desired result. (Then you also don't need `return c`, and can change the signature to `void value(int *a)`.) – Amadan Feb 07 '22 at 05:21

1 Answers1

5

You have 2 ways of having a function change data outside it: you can pass a variable by reference and update it, or you can return a value and use that. You are mixing half of each method instead. I reordered the code to make my commentary clearer.

#include <stdio.h>

int value(int *a);

// here you are passing by reference, good
int value(int *a){
    int c = (*a)*10; // but you don't change a
    return c;  // instead you return a new value
}

int main(){
    int num = 4;
    value(&num); // but here you ignore the new value. 
    printf("value of number is = %d", num);
    return 0;
}

to make reference way work, you need:

#include <stdio.h>
    
void value(int *a);
    
int main(){
    int num = 4;
    value(&num);
    printf("value of number is = %d", num);
    return 0;
}

void value(int *a){
    *a = (*a)*10; // change the value that a points at
    // no need to return anything
}

or to make the return way work:

#include <stdio.h>
// no need to pass by reference
int value(int a);
    
int main(){
    int num = 4;
    num = value(num); // use value return by function
    printf("value of number is = %d", num);
    return 0;
}

int value(int a){
    int c = a * 10;
    return c;
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    `pass by reference`: [C doesn't have pass by reference](https://stackoverflow.com/questions/2229498/passing-by-reference-in-c/30519731#30519731), a more appropriate term would be "pass a pointer to the function". – JASLP doesn't support the IES Feb 07 '22 at 05:35
  • 1
    @JustASimpleLonelyProgrammer you have a lot of people to persuade about that https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm https://www.ibm.com/docs/en/i/7.1?topic=calls-pass-by-reference https://dev.to/mikkel250/passing-by-value-passing-by-reference-in-c-1acg this progamming idiom is called 'passing by refernce' – pm100 Feb 07 '22 at 05:39
  • 1
    @pm100 sure, doesn't change the fact that all parameters in C is pass by value. – Fredrik Feb 07 '22 at 05:46
  • 1
    @Fredrik indeed, but the programming idiom is called 'pass by reference'. In the same way the c doesn't support strings, just a pointer to a character, but we all call them strings and most of the functions that manage them are called strxxx – pm100 Feb 07 '22 at 05:50
  • 2
    @pm100 to be pedantic the C standard does infact define string as a sequence of characters ending in a NULL character. – Fredrik Feb 07 '22 at 05:52
  • 1
    @pm100 I'm aware what "passing by reference" is but when you're passing a pointer to the function, you're still *passing the pointer by value* and that's why IMO, "passing a pointer" is more appropriate. – JASLP doesn't support the IES Feb 07 '22 at 16:08