0

As the question says , i have this example that i tried to make as close as possible to my code :

double a = 0.0;
double *dxda1=&a;

void cof(double x, double a, double b, double* y, double* dxda) 
{
    x = pow((a * b) / 4, 2);
    y = &x;
    *dxda = (a / 8) * pow(b, 2);
}   

void deriv() 
{
    double x =0.0;
    double y = 0.0;
    double dxda = 0.0;
    double a = 50;
    double b = 78;
    cof(x, a, b, &y, &dxda);
    dxda1 = &dxda;
}

int main()
{
    deriv();
    printf("whatineed: %g \n", *dxda1);
}

This example gives me the correct result which is 38025. But i would like to have to store the global variable and later print it in main without having to call the function deriv(). Is that possible?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Amyle
  • 11
  • 1
  • 4
    `dxda1 = &dxda;` - the global `dxda1` will hold the address of the local `dxda` and once the function returns it will become a dangling pointer. – wohlstad Mar 22 '23 at 13:59
  • 1
    `double dxda = 0.0;` defines a *local* variable inside the `deriv` function. Its life-time will end when the function return. Any pointer to that variable will then become invalid. – Some programmer dude Mar 22 '23 at 13:59
  • Dereferencing a pointer to retired variable `printf("whatineed: %g \n", *dxda1);` invokes *undefined behavior*. – MikeCAT Mar 22 '23 at 13:59
  • You saved the result to this question. Then, let's print that without calling `deriv()`: `printf("whatineed: %g \n", (double)38025);` – MikeCAT Mar 22 '23 at 14:00
  • 1
    Similar problem with `y = &x;` in the `cof` function. The solution for this function is to dereference the pointer to set the value it points to: `*y = x;` (which you do for the `dxda` variable on the next line of the `cof` function). – Some programmer dude Mar 22 '23 at 14:00
  • 3
    Now the big question for you: Why are you using global variable to begin with? Why are you using pointers? *Return* the results from the function instead. – Some programmer dude Mar 22 '23 at 14:01
  • I need a global variable because i have a different function that i am working on that returns both *y and *dxda. In order to make the cof() not be called so many times , i just wanted to call it once and retrieve store dxda in a global variable dxda1 . The main function has other stuff in it but i need to access dxda1 again without having to call the deriv function. I hope that makes sense. @Someprogrammerdude – Amyle Mar 22 '23 at 14:06
  • 1
    If you don't call `deriv()`, then how is the wanted value ever going to be computed? You could move the computation to `main()`, but then you would not need a global variable -- a local variable in `main()` would do. – John Bollinger Mar 22 '23 at 14:06
  • 2
    You can simply save the result to a global `double` variable without using pointers. – MikeCAT Mar 22 '23 at 14:08
  • And can you still have the correct result without calling the deriv function in main? @MikeCAT – Amyle Mar 22 '23 at 14:11
  • @Amyle Yes, by calculating the "correct result" using another program and embedding the result in your main program. – MikeCAT Mar 22 '23 at 14:16
  • 3
    To "return" multiple values, either do as in `cof` and emulate pass-by-reference. Or if the values are tightly or closely related, use structures and return the structure object. There's no need for global variables in your code as far as I can see. – Some programmer dude Mar 22 '23 at 14:19
  • 2
    Side note: `x * x` is pretty much *always* better than `pow(x, 2)` for floating-point `x`. For integer `x`, you might or might not prefer `x * (double) x` instead, depending on the circumstances. – John Bollinger Mar 22 '23 at 14:22
  • The parameter `x` in `cof` function is entirely meaningless, first thing you do with is overwriting it anyway... Considering all the comments so far, your function might rather look like: `void cof(double a, double b, double* y, double* dxda) { double tmp = a * b / 4; *y = tmp*tmp; *dxda = a / 8 * b * b; }` and usage of can be simply `double y; double dxda; int main() { cof(50.0, 78.0, &y, &dxda); /* now use y and dxda elsewhere */ return 0; }` – Aconcagua Mar 22 '23 at 14:30
  • You can even spare the local variable if you invert order of calculation: `*dxda = a / 8 * b * b; a = a * b / 4; *y = a * a;` – Aconcagua Mar 22 '23 at 14:35
  • 1
    Side note: You cannot *'call'* variables, you only can use them (read or write)... – Aconcagua Mar 22 '23 at 14:42
  • It sounds like the real question here is, "I have a global variable that I want to initialize with a complicated value which I need some code — in function `deriv` — to compute." And you want to have function `deriv` be called implicitly when the program starts running (so it behaves just like other global initializers) *without* having to call it explicitly. – Steve Summit Mar 22 '23 at 15:56
  • And that's a good (though frequent) question. I think there's a canonical answer somewhere. Two partial answers are [here](https://stackoverflow.com/questions/7869779) and [here](https://stackoverflow.com/questions/4880966). This sort of thing is well-defined in C++, and many compilers (including gcc) have decently-clean extensions to let you do the same sort of thing in C. – Steve Summit Mar 22 '23 at 15:59

0 Answers0