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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing array through a function

Status
Not open for further replies.

manvid

Programmer
Apr 1, 2002
37
IN

hi ,
i am facing problem passing array to a function .
my function prototype is

void function(array_type **array_name,....)
{
....
array_name[j][k] = ...

}

when i am trying to call this function with an actual parameter which is declared as

array_type array_name[rows][cols]

i am getting a segmentation fault . Inside the function
the 2-d array is being treated as a 1-d dimension so if
i do
array_name[j*..+k*..] instead of array_name[j][k]
it is working .
i am unable to figure out why this is happening ?
 
The problem is in C. It only accepts one variable dimension in arrays. To get around this

1) pass the array round as a 1D array
2) compute the 2D bits using a macro
Code:
int array[20000];
#define XMAX 100
#define ARRAY(y,x) array[y * XMAX + x]

Just use ARRAY(y,x) instead of array[y][x]

This technique also works for 3D and 4D arrays.
 
If you don't post sample code we can't guess very accurately
at your problem.
A segfault occurs when accessing memory you didn't allocate.

Looks like your array indice syntax is questionable, and as I
said, your approach is unclear.


 
xwb: please read K+R book, thanks
manvid: the array definetion ??
marsd: i agree.
 

hi ,
sorry for not submitting sample code . the array defination is local and i am pasing a locally declared array
to a function

the sample code is

#include<stdio.h>

#define ROWS 10
#define COLS 10

void Assign(int **,int,int);

int main(void)
{
int Array[ROWS][COLS];
Assign((int **)Array,ROWS,COLS);
/* got a segmentation falut */

}



void Assign(int **Array_name,int rows,int cols)
{
int k,j;
for(k=0 ; k<rows ; k++)
for(j=0 ; j<cols; j++)
{
Array_name[ k ][ j ] = 10;
}
}

this code is working for k=0 and as soon k becomes 1 it is giving a segmentation fault.

bye.
 
#include<stdio.h>
#define ROWS 10
#define COLS ROWS
#define NEXT ROWS
#define MORE ROWS

int main(int argc,char **argv)
{
int rows, cols, next, more, array[ROWS][COLS][NEXT][MORE];

for(rows = 0; rows< ROWS; rows++)
for(cols = 0; cols< COLS; cols++)
for(next = 0; next< NEXT; next++)
for(more = 0; more< MORE; more++)
array[rows][cols][next][more] = rows+cols+next+more;

for(rows = 0; rows< ROWS; rows++){
for(cols = 0; cols< COLS; cols++){
for(next = 0; next< NEXT; next++){
for(more = 0; more< MORE; more++)
printf(&quot;%4d &quot;,array[rows][cols][next][more]);
printf(&quot;\n&quot;);
}
printf(&quot;\n&quot;);
}
printf(&quot;\n&quot;);
}
exit(0);
}
/**************************************************
this example show you a FOUR dimensional int array.
if you want do it by functions (out of 'main()') you
need knowledge about pointer and pointer of pointer
this is not really hard to do, i have no time for
this, above code is a 5 minutes work... the K+R
book is a good teacher, the question is, WHY you
(believe to) need more-dimensional arrays ??
I NEVER needed in the past 25 years, a more-C-dimensional
array... C works simpler, it can do all what you want
in a simple one-dimensional array. so re-think your
code, make it simpler, do not expect a compiler
will translate your complex code, please help him.
YOU, not the compiler, ARE INTELLIGENT; prouve it. :))
**************************************************/
 
What you need to do is define the size of the second dimension of the array so the compiler knows how to size the offsets when you give it a dimension. The second array dimension is actually ROWS, not COLS. The following will do what you want...
[tt]
#include<stdio.h>

#define COLS 10
#define ROWS 10

void Assign(int Array_name[][ROWS],int,int);
void Print_Array(int Array_name[][ROWS],int,int);

int main(void)
{
int Array[COLS][ROWS];

Assign(Array,COLS,ROWS);

Print_Array(Array,COLS,ROWS);

return(0);
}

void Assign(int Array_name[][ROWS],int cols,int rows)
{
int k,j;

for(k=0 ; k<cols ; k++)
for(j=0 ; j<rows; j++)
{
Array_name[ k ][ j ] = (k*100)+j;
}
}

void Print_Array(int Array_name[][COLS],int cols,int rows)
{
int k,j;

for(k=0 ; k<cols ; k++)
{
for(j=0 ; j<rows; j++)
{
printf(&quot;%04d &quot;, Array_name[k][j]);
}
printf(&quot;\n&quot;);
}
}
[/tt]
I set the value it's being initialized to to a number that shows the row and column of that array element. The output looks like...
[tt]
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009
0100 0101 0102 0103 0104 0105 0106 0107 0108 0109
0200 0201 0202 0203 0204 0205 0206 0207 0208 0209
0300 0301 0302 0303 0304 0305 0306 0307 0308 0309
0400 0401 0402 0403 0404 0405 0406 0407 0408 0409
0500 0501 0502 0503 0504 0505 0506 0507 0508 0509
0600 0601 0602 0603 0604 0605 0606 0607 0608 0609
0700 0701 0702 0703 0704 0705 0706 0707 0708 0709
0800 0801 0802 0803 0804 0805 0806 0807 0808 0809
0900 0901 0902 0903 0904 0905 0906 0907 0908 0909
[/tt]
Also, if you have more dimensions, you need to define their size as well.

I think your confusion is that a two dimensional array of integers ([tt]int array[][][/tt]) is not exactly the same as a pointer to a pointer of ints ([tt]int ** array[/tt]). In some contexts you can use them the same way, but in reality the compiler sets up very different internal memory structures for each. The code above should do what you're trying to do though.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top