1

I am a bit confused with this type of C code:

void double_function(double **arr){
printf("Value at 1: %f \n", arr[1]);
}

int main() {

double arr[3] = {0.11,1.2,2.56};
double_function(&arr);

}

This does not print the value 1.2. I tried *(arr)[1] and (*arr[1]) as well, and I can't seem to access it. I keep getting:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Can someone help clarify this notation on how to access the array?

Please note that the specifications require that the function take a double **arr.

2
  • Your specification doesn't allow for sending what you're trying to send. Can you add more details on what the function is supposed to do? Commented Apr 3, 2021 at 21:08
  • I am supposed to take a double **arr and copy elements from a second array that is also inputed into the function. I Have boiled down my question here to being able to modify and access elements in the function from the double **arr. Commented Apr 3, 2021 at 21:10

4 Answers 4

5

The variable arr is an array of doubles and you clarified that you wanted to pass it in a pointer to pointer. Using temporary (double *) { arr } so we pass in its address with &(double *) { arr }:

#include <stdio.h>

void double_function(double **arr){
    printf("Value at 1: %f \n", (*arr)[1]);
}

int main() {
    double arr[3] = {0.11,1.2,2.56};
    double_function(&(double *) {arr});
}
Sign up to request clarification or add additional context in comments.

4 Comments

Can you tell the difference in what happens in stack frame when you apply this solution?
Both op and my solution passes in a pointer to function. The difference is what the pointer points at (address of the array, or address of an address of the array). Both are local stack allocated variables so the addresses are probably similar but you can deference the pointer and see what you get. Did that answer your question?
Thank you for explanation. Actually I understand later that that is nothing to do with the stack frame. What I am curious is that why I can not pass address of the address of the array with only &arr instead of &(double*){arr} ?
double_function expects the argument arr to be a double ** or a pointer to a pointer to a double. In main(), &arr is of type double (*)[3] or an array of 3 pointers to double which is very different thing than double **. {arr} is a temporary variable of type double *[1] or an array of 1 pointer to a double, it's cast to a double pointer with (double *) and then you take the address as usual with & so you have a double **.
1

The parameter passes to the function doesn't match the parameter's type, and you're using the parameter in the function incorrectly.

The function is defined to accept a double ** parameter. Inside the function, it passes arr[1], which has type double *, to printf using the %f format specifier which expects a double. Also, you're passing a double (*)[3] to the function, i.e. a pointer to an array, which doesn't match the parameter type.

If you want to pass an array of double to a function, the parameter type should be double *, since an array in most contexts decays to a pointer to its first element.

So change the function's parameter type to double *:

void double_function(double *arr){

And pass the array to the function directly:

double_function(arr);

Comments

0

add a pointer to the array like this:

#include <stdio.h>

void double_function(double **arr){
    
printf("Value at 1: %lf \n", (*arr)[1]);
}

int main() {

double arr[3] = {0.11,1.2,2.56};
double *n=arr;
double_function(&n);

}

Comments

0

Allan Wind already provides a nice a clean (as much as it can be) solution, I would add that the requirement of using a pointer to pointer parameter to take a 1D array argument makes little sense when you can use a simple pointer. Look how much more simple it looks:

#include <stdio.h>

void double_function(double *arr)
{
    printf("Value at 1: %f \n", arr[1]);
}

int main()
{
    double arr[3] = {0.11, 1.2, 2.56};
    double_function(arr);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.