1

I have written code below that contains a function with a pointer to a double array of size 3.My problems is this: when I pass the address of pointer to a double variable (that clearly isn't an array) and then want to change the value of this double variable in function "f" as written below, when I implement in this manner, the result is correct and the value of the variable is changed:

#include <stdio.h>
void f(double (*)[3]);
double a = 7.5;

int main()
{
   double* b = &a;
   f(&b);
   printf("a = %lf\n", a);
   return 0;

}
void f(double (*hi)[3])
{
   double **sth = (double **) hi;\
   *(*sth) = 1;

}

But when I implement as below the value isn't changed:

void f(double (*hi)[3]){
    (*hi)[0] = 1;
}

Any idea and suggestion is surely appreciated.

4
  • 1
    If you use pointers to arrays, you are almost certainly doing it wrong. In C, you pass a pointer to the first element of an array to functions, not a pointer to an array. Commented Oct 10, 2015 at 20:04
  • 1
    Also, converting a pointer to an array to a pointer to pointer and then dereferencing the result is undefined behaviour. Don't do that. Commented Oct 10, 2015 at 20:06
  • 1
    A pointer to array and a pointer to pointer are not the same thing. In addition you switch off compiler warnings by casting a spell. Don't do that, good C code works without casts. The suggestion would be, don't use casts, sort out your types. Commented Oct 10, 2015 at 20:07
  • Looks like you'll have to work on your understanding on arrays and pointers -- therefore please update your question. To get your code to consider this: (1) change f(&b); to f(b); .. (as you want to pass the pointer, not the address of the pointer) (2) change double (*hi)[3] to double (*hi). Commented Oct 10, 2015 at 20:39

1 Answer 1

1

First fix the compilation errors your program is giving. After fixing those you will know the problem.

prog.c: In function 'main':
prog.c:8:6: error: passing argument 1 of 'f' from incompatible pointer type [-Werror=incompatible-pointer-types]
    f(&b);
      ^
prog.c:2:6: note: expected 'double (*)[3]' but argument is of type 'double **'
 void f(double (*)[3]);

http://ideone.com/mueyCU

#include <stdio.h>
void f(double (*)[3]);
double a = 7.5;

int main()
{
    double* b = &a;
    f(b);
    printf("a = %lf\n", a);
    return 0;

}

void f(double (*hi)[3]){
    (*hi)[0] = 1;
}

Above code is not the right way to do things and should be avoided at all costs.

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

7 Comments

actually I want to understand the syntax I have written above! I know it is complicated and there are much better ways than my code but I do not want the result with another code!!!
@Mehdi: do you know what is this double (*hi)[3]?
I think this is a pointer to an array of double type @noman pouigt
@Mehdi: I have edited the answer.Have a look and regarding your answer it is not right. double (*hi)[3] is a pointer to an array[3] of type double.
yes it works! but logicly we should send &b to function f, isn't it? @noman pouigt
|

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.