NO

Author Topic: Cannot pass 2-D array to a function - Please help  (Read 4389 times)

boral

  • Guest
Cannot pass 2-D array to a function - Please help
« on: September 21, 2014, 06:34:35 AM »
I am learning to pass arrays to functions. My problem is this : I will scan a 2-D array in main() function. Pass the order n of the matrix and the address of first element to a function called pass(). Then I will print all the elements in pass().

I cannot understand what is wrong with my code. Please point it out. Any sort of help is welcome.

My code :

Code: [Select]
# include<stdio.h>

main()
{
float a[50][50];
int i, j, n ;
void pass( int , float* ) ;
printf("Enter n : ") ;
scanf("%d", &n) ;

for( i = 0; i < n ; i++)
{
for( j = 0; j < n ; j++)
{
printf("\n Enter a%d%d = ", i, j) ;
scanf("%f", &a[i][j]) ;
}
}

pass(n, &a[0][0]) ;

}

void pass( int n, float *a_pointer )
{
int i, j ;
for( i = 0; i < n ; i++)
{
for( j = 0; j < n ; j++)
{
printf("\n\n %f", *(a_pointer + n*i + j )) ;
}
}
}

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Cannot pass 2-D array to a function - Please help
« Reply #1 on: September 21, 2014, 06:54:11 AM »
Well, beside the way you define the main() function, your basic problem with the code is "scope".

You are trying to forward declare the pass() function with the scope of the main() function (as delimited by the curly braces of main(){...}. Any such forward declaration needs to be outside of main(). Or simply get used to put the main() function always at the end of the code...

Ralf

boral

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #2 on: September 21, 2014, 08:37:58 AM »
Thanks for your reply.

But I still cannot understand your point properly. Can you please correct my code and post it here.

laurro

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #3 on: September 21, 2014, 10:29:32 AM »

Code: [Select]
#include<stdio.h>
void pass(int, int, float *);

int main(void)
{
float a[50][50];
int x, y, n;

printf("Enter n : ");
scanf("%d", &n);

for (y = 0; y < n; y++)
{
for (x = 0; x < n; x++)
{
printf("\n Enter a%d%d = ", y, x);
scanf("%f", &a[y][x]);
}
}
printf("\n\n");

int stride = sizeof(a[0]) / sizeof(a[0][0]);
pass(n, stride, &a[0][0]);

return 0;
}

void pass(int n, int stride, float *a_pointer)
{
int x, y;
for (y = 0; y < n; y++)
{
for (x = 0; x < n; x++)
{
printf(" %.4f ", *(a_pointer + (y * stride) + x));
}
printf("\n\n");
}
}

Beside this, what Ralf already said.

Laur

boral

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #4 on: September 21, 2014, 04:35:15 PM »
Thank you very much. But one more thing. Why does we require the variable stride ? What is it exactly doing ?

JohnF

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #5 on: September 21, 2014, 05:52:59 PM »
In your the example stride == 50 which is used to find the address of the next row of floats.

You might find it easier to us pass the array like so

void pass(int, float a_pointer[][50]);

Code: [Select]

#include <stdio.h>

void pass(int, float a_pointer[][50]);

int main(void)
{
float a[50][50];
int x, y, n;

printf("Enter n : ");
scanf("%d", &n);

for (y = 0; y < n; y++)
{
for (x = 0; x < n; x++)
{
printf("\n Enter a%d%d = ", y, x);
scanf("%f", &a[y][x]);
}
}
printf("\n\n");

pass(n, a);

return 0;
}

void pass(int n, float a_pointer[][50])
{
int x, y;
for (y = 0; y < n; y++)
{
for (x = 0; x < n; x++)
{
printf("%.4f\n", a_pointer[y][x]);
}
}
}

John

laurro

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #6 on: September 21, 2014, 09:58:18 PM »


The stride is the number of elements(in this case floats) in one row (one horizontal line).
Let's simplify your example mainly we make your array smaller 5 by 5 elements: float a[5][5];.
How you thing the array it will look (in memory) ? something like :

I   row   |  00, 01, 02, 03, 04,
II  row   |  10, 11, 12, 13, 14,
III row   |  20, 21, 22, 23, 24,
IV  row   |  30, 31, 32, 33, 34,
V   row   |  40, 41, 42, 43, 44

wrong, there are not such things like 2D, 3D ... nD organizations in memory, only
one dimensional layout of bytes (4 bytes for a float) from lower to higher addresses.
Our array it will be:

I row               II row              III row             IV row              V row
<---------------->  <---------------->  <---------------->  <---------------->  <---------------->
00, 01, 02, 03, 04, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44

better

I row               II row              III row             IV row              V row
<---------------->  <---------------->  <---------------->  <---------------->  <---------------->
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 (25 elements)


// and because one float take 4 bytes
//
// I row                                                                                                      II row
// <------------------------------------------------------------------------------------------------------> <--.......
// 00,                  01,                  02,                  03,                  04,                  05,  ...
// starting at address:
//    a_pointer
//        +
//     offset:
// 0x00                 0x04                 0x08                 0x0c                 0x10                 0x14 ...
// 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f, 0x10,0x11,0x12,0x13....
// -------------------|--------------------|--------------------|--------------------|-------------...
// float               float                float                float                float ...
             

So the short answer
the stride is <----------------> = 5 (50 in your original code) because you access your array with a float pointer.

Let's say you want to access the a[2][3] element (get or set)

         stride = 5
    <--------x------->
 |  00, 01, 02, 03, 04,
 |  10, 11, 12, 13, 14,
 y  20, 21, 22, 23, 24,
 |  30, 31, 32, 33, 34,
 |  40, 41, 42, 43, 44

Remember the indexes in C starts from 0, and a_pointer point to a[0][0]. So you want a[y=2][x=3]
value = value at (a_pointer + (stride * y) + x)
      = value at (a_pointer + (5 * 2) + 3)
      = value at (a_pointer + 13 )
      = *(a_pointer + 13 )

<---------------->  <---------------->  <---------------->  <---------------->  <---------------->
00, 01, 02, 03, 04, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44
  ->  ->  ->  ->  ->  ->  ->  ->  ->  ->  ->  ->  ->
   1   2   3   4   5   6   7   8   9  10  11  12  13

Laur

boral

  • Guest
Re: Cannot pass 2-D array to a function - Please help
« Reply #7 on: September 29, 2014, 09:36:48 AM »
Thanks to all of you for this huge amount of help. It clears the concept thoroughly.