passing multiple-index array to function

J

Jay G. Scott

this should be trivial, yet i can't find it in a tutorial.

int AAA[12][234];
int BBB[98][2478];

i want to call f(AAA,12,234) and f(BBB,98,2478).

how should f be declared?

int f( huh?, int ll, int rr)
{
int a,b;
for( a=...
for( b=...
huh2?[a] = 234.4;
return 0;
}

i'm starting to get the impression that f() can't use int **huh,
because that's pointers to pointers, whereas AAA is 12*234
contiguous ints. i really do want to pass f the address of ll*rr
contiguous ints so i don't have to "hardcode" for each array
size i might use.

what's the right way? i won't be doing 3 indexes, but is
the generalization to 3 obvious?

i gotta be missing something.

j.
 
M

Martin Ambuhl

Jay said:
this should be trivial, yet i can't find it in a tutorial.

int AAA[12][234];
int BBB[98][2478];

i want to call f(AAA,12,234) and f(BBB,98,2478).

int f(int n, int m, int a[n][m]); /* C99 prototype */
 
J

Jay G. Scott

Jay said:
this should be trivial, yet i can't find it in a tutorial.

int AAA[12][234];
int BBB[98][2478];

i want to call f(AAA,12,234) and f(BBB,98,2478).

int f(int n, int m, int a[n][m]); /* C99 prototype */


thank you. i have written this in my C manual.

j.
 
P

Pedro Graca

Jay said:
int AAA[12][234];
int BBB[98][2478];

i want to call f(AAA,12,234) and f(BBB,98,2478).

how should f be declared?
what's the right way?

Is there anything wrong in passing a void *, provided, of course, that
everything is properly documented?


#include <stdio.h>
#include <stdlib.h>

double
sum_int_array_elements(
void *two_dimensional_array_of_ints,
size_t elements);

int main(void) {
int a[3][7] = {
{1, 2, 3, 4, 5, 6, 7},
{1, 2, 3, 4, 5, 6, 7},
{1, 2, 3, 4, 5, 6, 7}
};
int b[9][2] = {
{1, 2}, {1, 2}, {1, 2},
{1, 2}, {1, 2}, {1, 2},
{1, 2}, {1, 2}, {1, 2}
};

printf("sum of elements of a is %g\n", sum_int_array_elements(a, 3 * 7));
printf("sum of elements of b is %g\n", sum_int_array_elements(b, 9 * 2));

return 0;
}

double sum_int_array_elements(void *a, size_t n) {
double sum = 0;
int * p = a;

while (n--) sum += p[n];

return sum;
}
 
J

Jay G. Scott

Jay said:
int AAA[12][234];
int BBB[98][2478];

i want to call f(AAA,12,234) and f(BBB,98,2478).

how should f be declared?
what's the right way?

Is there anything wrong in passing a void *, provided, of course, that
everything is properly documented?

i left out a lot of detail.
the soln below turns the array into a scaler---it treats all the elements
alike. i'm going to return an array value.
if i lose the information about indexes then i'd have
to do something to assign to a given row and column. i'd rather let
the compiler keep track of the indexes.

j.
#include <stdio.h>
#include <stdlib.h>

double
sum_int_array_elements(
void *two_dimensional_array_of_ints,
size_t elements);

int main(void) {
int a[3][7] = {
{1, 2, 3, 4, 5, 6, 7},
{1, 2, 3, 4, 5, 6, 7},
{1, 2, 3, 4, 5, 6, 7}
};
int b[9][2] = {
{1, 2}, {1, 2}, {1, 2},
{1, 2}, {1, 2}, {1, 2},
{1, 2}, {1, 2}, {1, 2}
};

printf("sum of elements of a is %g\n", sum_int_array_elements(a, 3 * 7));
printf("sum of elements of b is %g\n", sum_int_array_elements(b, 9 * 2));

return 0;
}

double sum_int_array_elements(void *a, size_t n) {
double sum = 0;
int * p = a;

while (n--) sum += p[n];

return sum;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top