Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sending Part of 2D array to function

rasred2004rasred2004 Member Posts: 2
Dear all

i found a code in this website that let me send a two dimensional array to a function using pointer , but my problem is how to send part of this array without creating a new array ( the size of the new array equal to the a size of the array i want to send it )

the code show below

in the code i can send the array2d from main() to print_2_array() but if i want to send part of the array ( for example i want to send the array

A [FROM 3 TO 10][FROM 3 TO 20]

is there any way i can send this array to the function without creating a new array of size ATEMP[7][17] and made it equal to the A [FROM 3 TO 10][FROM 3 TO 20] , because if i decalare a new array i must allocate to them a memory location ( which is not good)

#include
#include
#include

#define X 10
#define Y 20

void print_2_array(int** array, int num_rows, int num_cols)
{
int i, j;

for (i=0; i < num_rows; i++)
{
for(j=0; j < num_cols; j++)
printf("%d ", array[i][j]);
printf("
");
}
}

void cleanup(int** array, int x)
{
int i;

for(i=0; i<x; i++)
free(array[i]);
free(array);
}

int main(int argc, char *argv[])
{
int i, j;
int** array2d = (int**)malloc(X * sizeof(int*));

if(!array2d)
{
printf("Not enough memory.
");
exit(1);
}

/*
Set all pointers to NULL.
This will make it possible to call free() if
out of memory during data allocation.
*/
memset(array2d, 0, X * sizeof(int*));

for(i=0; i<X; i++)
{
array2d[i] = (int*)malloc(Y * sizeof(int));
if(!array2d[i])
{
printf("Not enough memory.
");
cleanup(array2d, X);
exit(1);
}
}

/* fill the arrays with something */
for(i=0; i<X; i++)
for(j=0; j<Y; j++)
array2d[i][j]=j;

print_2_array(array2d, X, Y);

cleanup(array2d, X);

return 0;
}


with best wishes

and i will be very thankfull for you reply or any tips that can help me with this problem

Comments

  • LundinLundin Member Posts: 3,711
    I'm not sure if I understand you... did you have something like this in mind?

    [code]void do_things_with_array (int* array, int start, int end)
    {
    ...
    }


    for(i=0; i<y; i++)
    {
    do_things_with_array(array2d[i], start, end);
    }
    [/code]


    I also [italic]strongly[/italic] recommend [link=http://c-faq.com/aryptr/index.html]this link[/link]. Multi-dimensional arrays in C is a much more complex topic than what people think. For example, statically allocated multi-arrays are -not- compatible with pointer-to-pointer dynamically allocated multi-arrays, as the one in your example.
  • rasred2004rasred2004 Member Posts: 2
    Dear sir
    thank you for reply , i will explain more

    if you have a matrix A

    A = { 1, 2,3,4,5;
    1,2,3,4,5;
    1,2,3,4,5;
    1,2,3,4,5;
    1,2,3,4,5;}

    and you want to send a smaller matrix to a function using pointer ( for example you want to send the matrix

    {3,4,5;
    3,4,5;
    3,4,5;}
    which is part of matrix A [2 to 4][2 to 4] , how can i send it

    by the way if i used the way you suggest it will be waste of time ( because in my case i have the matrix A with ddimension 2024*2024) and the dimension is reduced by 1 every time i call the function ( it will be 2023*2023)) so every call i need to use a for loop , this is way i asked is there a way to send part of the matrix ( by pointer)


    with best wishes







    : I'm not sure if I understand you... did you have something like this
    : in mind?
    :
    : [code]: void do_things_with_array (int* array, int start, int end)
    : {
    : ...
    : }
    :
    :
    : for(i=0; i<y; i++)
    : {
    : do_things_with_array(array2d[i], start, end);
    : }
    : [/code]:
    :
    :
    : I also [italic]strongly[/italic] recommend
    : [link=http://c-faq.com/aryptr/index.html]this link[/link].
    : Multi-dimensional arrays in C is a much more complex topic than what
    : people think. For example, statically allocated multi-arrays are
    : -not- compatible with pointer-to-pointer dynamically allocated
    : multi-arrays, as the one in your example.


  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Dear sir
    : thank you for reply , i will explain more
    :
    : if you have a matrix A
    :
    : A = { 1, 2,3,4,5;
    : 1,2,3,4,5;
    : 1,2,3,4,5;
    : 1,2,3,4,5;
    : 1,2,3,4,5;}
    :
    : and you want to send a smaller matrix to a function using pointer (
    : for example you want to send the matrix
    :
    : {3,4,5;
    : 3,4,5;
    : 3,4,5;}
    : which is part of matrix A [2 to 4][2 to 4] , how can i send it
    :
    : by the way if i used the way you suggest it will be waste of time (
    : because in my case i have the matrix A with ddimension 2024*2024)
    : and the dimension is reduced by 1 every time i call the function (
    : it will be 2023*2023)) so every call i need to use a for loop , this
    : is way i asked is there a way to send part of the matrix ( by
    : pointer)
    :
    :
    : with best wishes
    :
    :

    Probably the way to do this is to send the entire matrix and then the start and end points for the submatrix.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.