0

I know that if we call a function using "call by value" we cant overwrite the value we are passing to the function inside the function, and if the function returns a pointer value, we must not try to return the address of a local variable (local to the function) because this local variable gets distroyed after the function is executed (and this results in a warning), but look at this code:

#include <stdio.h>
int *fun(int x,int y, int c[]){
    c[0]=100;
    x=887;
    return &c[0];
}

int main()
{
    int x=10,y=20,*z;
    int c[]={1,2,3,4,5};
    z=fun(x,y,c);
    printf("%d\n",*z);
    printf("%d\n",c[0]);
    printf("%d\n",x);

    return 0;

The output is :

100 100 10

So, we are not changing the value of "x" (main function x) but we are actually changing the value of c[0] (main function c[0]), and for what i know this should not be possible because im using call by value and not by reference, can you explain this to me please?

And also, if i change the line of code "return &c[0];" for this line of code "return &x;" i dont get a warning in the first statement but i get a warning in the second, why does this happens?

3
  • 2
    "why can we return the address of this array element?" Because the array isn't local to the function: it was passed by the caller. Commented Sep 17, 2023 at 17:23
  • 3
    "I'm using call by value and not by reference." An array is never passed to a function (except when it is a struct member). The array decays to a pointer to its first element, and that pointer is passed to the function. So int *fun(int x,int y, int c[]) is identical to int *fun(int x,int y, int *c) Commented Sep 17, 2023 at 17:26
  • 1
    The return &x; is doing what you previously said you know you must not do: return the address of a local variable. But return &c[0]; is OK because although the pointer is local to the function, what it points to is not. Commented Sep 17, 2023 at 17:29

1 Answer 1

2

As a pointer is referencing an object you can change that object by dereferencing the pointer. It does not matter where this object is (of course if the object exists - which is a problem when you return a pointer to the automatic storage duration).

im using call by value and not by reference, can you explain this to me please?

No, you pass reference to the first element to the array c defined in main. Arrays are not passed by value!!!

To avoid confusion change the function declaration to:

int *fun(int x,int y, int *c)

And also, if i change the line of code "return &c[0];" for this line of code "return &x;" i dont get a warning in the first statement but i get a warning in the second, why does this happens?

because you return reference to the first element of array c defined in function main which is not local to the function fun

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.