C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sending Part of 2D array to function Posted by rasred2004 on 19 Aug 2008 at 3:30 AM
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
Report
Re: sending Part of 2D array to function Posted by Lundin on 19 Aug 2008 at 3:56 AM
I'm not sure if I understand you... did you have something like this in mind?

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);
}



I also strongly recommend this 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.
Report
Re: sending Part of 2D array to function Posted by rasred2004 on 19 Aug 2008 at 6:40 AM
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?
:
:
: 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);
: }
: 
:
:
:
: I also strongly recommend
: this 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.


Report
Re: sending Part of 2D array to function Posted by BitByBit_Thor on 19 Aug 2008 at 8:02 AM
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.