i have the following chunk of code..
but it is giving seg fault while running..Code:struct covar covariant[zeta_i][zeta_j][zeta_k]; struct covar ***p_covariant; **p_covariant=&covariant[0][0][0];
can someone plzz help me with this..
i have the following chunk of code..
but it is giving seg fault while running..Code:struct covar covariant[zeta_i][zeta_j][zeta_k]; struct covar ***p_covariant; **p_covariant=&covariant[0][0][0];
can someone plzz help me with this..
Providing more code might be helpful. Is this what you might need?
Code:struct covar covariant[zeta_i][zeta_j][zeta_k]; struct covar (*p_covariant)[zeta_j][zeta_k] = covariant;
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
hmm..i m a little confused.. in this case.. if i have to pass the array to another function can i just pass the pointer?
i think this may work..
thanks..
Something like this, perhaps?
Code:void foo(struct covar p_covariant[zeta_i][zeta_j][zeta_k]) { } int main(void) { struct covar covariant[zeta_i][zeta_j][zeta_k]; /* ... */ foo(covariant); return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
It's not the correct type.Originally Posted by MDofRockyViewType mismatch (assignment) (struct covar *** = struct covar (*)[5][5])
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
@Dave_Sinkula:
your suggestion works.. but doesnt it reduce the efficiency of program since you have to pass the an array to a function.. my program needs very high efficiency and hence i am looking for a better way..
Arrays are never passed by value[*] since C can't; it is a pointer that is passed.
Try not to prematurely optimize: if you really need efficiency, profile to find the bottlenecks.
[*edit]Unless you consider the value of an array to be a pointer to the first element.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
MDofRockyView, read what Dave wrote - everything you've suggested is wrong in one way or another.
@zoso123
All these are the same as far as the compiler is concerned.
All result in a single pointer being passed to the array, and all can be accessed using p_covariant[x][y][z] notation just as if the array were being accessed in main.Code:void foo(struct covar p_covariant[zeta_i][zeta_j][zeta_k]) { } void foo(struct covar p_covariant[][zeta_j][zeta_k]) { } void foo(struct covar (*p_covariant)[zeta_j][zeta_k]) { }
None result in an entire array being copied.
The first one has the advantage of being a simple copy/paste of the array you want to pass to the function in the first place, so it's dead easy to remember how to do it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
That's a pointer to a single struct. This question has already been answered in this thread a couple times.Originally Posted by MDofRockyView
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*