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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#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("\n");
}
}
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.\n");
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.\n");
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