pointer to multidimensional array..

This is a discussion on pointer to multidimensional array.. within the C Programming forums, part of the General Programming Boards category; i have the following chunk of code.. Code: struct covar covariant[zeta_i][zeta_j][zeta_k]; struct covar ***p_covariant; **p_covariant=&covariant[0][0][0]; but it is giving seg ...

1. pointer to multidimensional array..

i have the following chunk of code..

Code:
```struct covar covariant[zeta_i][zeta_j][zeta_k];
struct covar ***p_covariant;
**p_covariant=&covariant[0][0][0];```
but it is giving seg fault while running..

can someone plzz help me with this..

2. 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;```

3. 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..

4. 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;
}```

5. Originally Posted by MDofRockyView
I'm not sure if this is correct, are you just trying to assign the pointer to the array? Does this work?
Code:
```struct covar covariant[5][5][5];
struct covar ***p_covar;
p_covar = covariant;```
It's not the correct type.
Type mismatch (assignment) (struct covar *** = struct covar (*)[5][5])

6. @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..

7. 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.

8. 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.
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]) {
}```
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.

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.

9. Originally Posted by MDofRockyView
How would you just assign a pointer to the 3d array? Is this incorrect?

struct structype *p_struct = &array[0][0][0];
That's a pointer to a single struct. This question has already been answered in this thread a couple times.