Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of pointers to mutidimensional arrays?

Status
Not open for further replies.

bigE

Programmer
Jun 26, 2000
2
0
0
US
I am trying to create an array of pointers to a set of two-dimensional arrays but cannot seem to get the thing working. Here's the function, which gets called six times. <br><br>void displacement_from_source(int X, int u_position[4][2],int* rx_displacement[6])<br>{<br> <br>int i,j;<br>int* rx;<br>rx=(int*)malloc(8*sizeof(int));<br>rx_displacement[X]=rx;<br><br> for (i=0;i&lt;4;i++)<br> {<br> for (j=0;j&lt;2;j++)<br> {<br> rx<i>[j]=u_position<i>[j];<br> printf(&quot;rx = %d,u= %d\n&quot;, rx<i>[j],u_position<i>[j]);<br> //printf(&quot;rx_displacement[X]=%d\n&quot;,rx_displacement[X]);<br> }<br> }<br>}<br><br>When I run this for a one dimensional array I have no problem. Obviously I am doing something stupid with the pointers to the two-dimensional arrays. I have tried all sorts of things but no luck. I would really appreciate some help. Thanks.<br><br>Ian (BigE)
 
Hi ,<br>&nbsp;&nbsp;First you tell the background of the function which you have written. What it should do and what should be&nbsp;&nbsp;the passed parameters. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bhaskar <p>Bhaskar Bora<br><a href=mailto:bhaskar_bora@yahoo.com>bhaskar_bora@yahoo.com</a><br><a href= > </a><br>___________________________________________________________<br>
C and C++ are too Cool ... Like CoSSAP ..<br>
Ooops What's this CoSSAP...?? <br>
It's a Objected oriented language created by me ...<br>
 
hi , <br>&nbsp;&nbsp;&nbsp;Here is the code which will surely help you ... <br>&nbsp;&nbsp;&nbsp;You have made many mistakes .. like you have use 'memcpy' for assigning memory , in-fact&nbsp;&nbsp;it is use to copy a memory block.<br>&nbsp;&nbsp;&nbsp;If you still have any problem then you are welcome.<br><br>#include &lt;stdio.h&gt; <br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>void displacement_from_source(int X, int u_position[4][2],int* rx_displacement[6]);<br>void print_out_arrays(int X, int* rx_displacement[6]);<br><br>// Main Program<br>void main ()<br>{<br><br>/* these are not global variables */<br><br>int i,j;<br>int u_position[4][2]={5,6,7,8,9,10,11,12};<br>int X;<br>int* rx_displacement[6]; //array of pointers<br>int*tx; <br>int temp_array[4][2];<br><br>for (X=0; X&lt;6; X++) // go through six times to form the six integer arrays<br>{<br>displacement_from_source(X,u_position,rx_displacement);<br>}<br><br>for (X=0; X&lt;6; X++) //go through six times to print arrays<br>{<br>tx=rx_displacement[X]; //assign pointer tx the first interarray address<br>memcpy(temp_array,tx, 8*sizeof(int)); //assign this address to temp_array<br><br>for (i=0;i&lt;4;i++)<br>{<br>for (j=0;j&lt;2;j++)<br>{ <br>printf(&quot;%d\n&quot;, temp_array[j]);<br>}<br>}<br>}<br><br>}<br><br>void displacement_from_source(int X, int u_position[4][2],int* rx_displacement[6])<br>{<br><br>int i,j;<br><br>int rx_array[4][2];<br>int rx_temp[4][2];<br>int* rx;<br>int* tx;<br>&nbsp;<br>memcpy(rx,rx_temp, 8*sizeof(int)); //assign the address of rx_temp to the pointer rx<br>rx_displacement[X]=rx; //assign the address of rx to the array of pointer's Xth<br>for (i=0;i&lt;4;i++)<br>{<br>for (j=0;j&lt;2;j++)<br>{<br>rx_temp[j]=u_position[j]; <br>}<br>}<br>}<br><br><br> <p>Bhaskar Bora<br><a href=mailto:bhaskar_bora@yahoo.com>bhaskar_bora@yahoo.com</a><br><a href= > </a><br>___________________________________________________________<br>
C and C++ are too Cool ... Like CoSSAP ..<br>
Ooops What's this CoSSAP...?? <br>
It's a Objected oriented language created by me ...<br>
 
void displacement_from_source(int X, int u_position[4][2],int*[RED]*[/RED] rx_displacement[6])<br>{ int i,j,k=0,int [RED]**arr=*[/RED]rx_displacement;<br>&nbsp;&nbsp;for (i=0;i&lt;4;i++)<br>&nbsp;&nbsp;{ for (j=0;j&lt;2;j++,k++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(k=X)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ arr[k]=&u_position<i>[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;rx_displacement[%d]=%d\n&quot;,X,rx_displacement[k]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>}<br>
 
void displacement_from_source(int X, int u_position[4][2],int** rx_displacement[6])<br>{ int i,j,k=0,int **arr=*rx_displacement;<br>&nbsp;&nbsp;for (i=0;i&lt;4;i++)<br>&nbsp;&nbsp;{ for (j=0;j&lt;2;j++,k++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(k=X)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ arr[k]=&u_position<i>[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;rx_displacement[%d]=%d\n&quot;,X,*(arr[k]));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>}<br>&nbsp;<br><br>pruébalo y escríbeme a <A HREF="mailto:rolandocente@latinmail.com">rolandocente@latinmail.com</A> si aún tienes dificultades.<br><br><br>&nbsp;<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top