3

I learn that in order to access or modify the value of a variable by calling a function, we need to pass pointers as arguments such as below :

#include <stdio.h>

//create a function 
void Increment(int* x) {    
   *x = *x+1;
   printf("Address of variable x in increment = %p\n",x); 

}

int main() {
   int a;
   a = 10;
   Increment(&a);   
   printf("Address of variable 'a' in main = %p\n",&a);
   printf("Value of a in main function = %d\n",a); 


}

But I did another test, and I found out that by calling the function and returning the value, I can also achieve the same result.

#include <stdio.h>

//create a function 
int Increment(int x) {        // do not use VOID
   x = x+1;
   printf("Address of variable x in increment = %p\n",x); 
   return x;                  

}

int main() {
   int a;
   a = 10;
       int hasil;
       hasil = Increment(a);   
   printf("Address of variable 'a' in main = %p\n",&a);
   printf("Value of a in main function = %d\n",hasil); 


}

My questions :

1) Do I have to pass pointers as argument if I can just use return value to achieve the same result?

2) I observe when I print the address of the memory of variable 'x' from the function that returns value, the memory address is very short 0xb , any idea why? normally the address is very long.

Lion Gordon
  • 87
  • 1
  • 6
  • What if you want to modify more than one variable? (Also they don't do the exact same thing - the second version copies the value of the variable) – UnholySheep Mar 21 '17 at 09:11
  • 2
    Returning the function result means you can use it in expressions; a void function cannot be used in expressions. – Paul Ogilvie Mar 21 '17 at 09:14

3 Answers3

5

1) Do I have to pass pointers as argument if I can just use return value to achieve the same result?

No, you don't always have to. But observes that it frees up the return value of the function for error reporting. A very simple example:

enum error_code {
  E_SUCCESS,
  E_GENERAL_FAILURE,
  E_MEMORY_ALLOCATION_FAILED,
  E_INVALID_ARGUMENT,
  E_FILE_NOT_EXIST
};

struct my_important_data {
  // stuff
};

enum error_code fill_important_data_from_file(char const *file_name,
                                              struct my_important_data **data)
{
  if(!data || !file_name)
    return E_INVALID_ARGUMENT;

  *data = malloc(sizeof(**data));

  if(!*data)
    return E_MEMORY_ALLOCATION_FAILED;

  // Return different error codes based on type of failure
  // so the caller can know what exactly went wrong

  return E_SUCCESS;
}

2) I observe when I print the address of the memory of variable 'x' from the function that returns value, the memory address is very short 0xb , any idea why? normally the address is very long.

That's because you are using the %p conversion specifier to print a regular int, and not an actual address. Strictly speaking that's causing the printf function to have undefined behavior.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
-1

You need to pass a pointer to a function when you need to modify the elements of an array. In C, you can't pass an array or return an array from a function.

Answer for your second question is, in C function argument are passed by value. That means the value of passed arguments is copied to parameters (which are local to the function) of the function. Therefore the address of passed argument and the address of function parameter are different.

haccks
  • 104,019
  • 25
  • 176
  • 264
-1

If parameter x is passed by point or reference, then there is no value copy of x is created. If not, there is a copy created. This is the difference.
Think about this situation, if the parameter is a complicate struct test, it is a waste of time to make a copy of test.
And if you pass x by point, you can use it as input and also as output.

Charles
  • 19
  • 6