0

i'm required to find the nth root by creating a function that uses a value pointed by pointer double *num passed as an argument to the function and returns a double pointer to the result i'm trying to pass an empty pointer and assign a value to it which is the result then dereference it getting back the result but there is a problem i can't figure

#include <math.h>

int main () {
double *returnPointer=NULL;
double *res=NULL;

double num=50;

double *rootnth(double *, int, double *);

res = rootnth(&num, 7, returnPointer);
printf("%lf", *res);

   return 0;
}

double *rootnth(double *num, int n, double *returnPointer) {
    *returnPointer= pow(*num,(float)1/n);
    return returnPointer;
}



Ahmad ghoneim
  • 844
  • 7
  • 13
  • 2
    Can you please add some punctuation (full stops) to your description? It's very hard to read in one long sentence. – kaylum Mar 23 '20 at 10:55
  • In rootnth, you are returning a `double**`. Use `return returnPointer;` instead... Furthermore, make sure returnPointer actually points to something. – Elijan9 Mar 23 '20 at 10:56
  • You dereferencing a NULL ponter here: `*returnPointer= pow(*num,(float)1/n);` – Eraklon Mar 23 '20 at 10:57
  • Compile with all warnings enabled and consider them as errors – Jabberwocky Mar 23 '20 at 10:59
  • `returnPointer` doesn't point at allocated memory, simple as that. You can't store anything at the address where it points (NULL). – Lundin Mar 23 '20 at 11:00

0 Answers0