In the same way as before. You have the number of rows and the number
of columns; they were arguments to the first function. You also have
the pointer to the array data. Just pass them to a second function,
declared much like the first. Example programs, with and without using
gcc's extensions for
array passing:
* * * GCC VERSION * * *
#include <stdio.h>
void func1(int rows, int cols, int data[rows][cols]);
void func2(int rows, int cols, int data[rows][cols]);
int main(int argc, char *argv[])
{
int data[10][20];
func1(10, 20, data);
/* Outputs 5 and 12, showing that both functions worked */
printf("%d %d\n", data[5][6], data[2][3]);
}
void func1(int rows, int cols, int data[rows][cols])
{
data[5][6] = 5; /* Do various things to data */
func2(rows, cols, data);
}
void func2(int rows, int cols, int data[rows][cols])
{
data[2][3] = 12; /* Do various other things to data */
}
* * * End * * *
An alternate program that does NOT use gcc's extensions to C:
#include <stdio.h>
/* This macro is a convenience; it works only if "cols" is set in the
function in which it is used. The gcc extensions really do much the
same thing, consider this a poor-man's version */
#define ROWCOL(array, row, col) (array)[(row) * (cols) + (col)]
void func1(int rows, int cols, int *data);
void func2(int rows, int cols, int *data);
int main(int argc, char *argv[])
{
int data[10][20];
/* Pass the address of the first element. Just passing 'data' would
produce a compile-time error. */
func1(10, 20, &data[0][0]);
/* Outputs 5 and 12, showing that both functions worked */
printf("%d %d\n", data[5][6], data[2][3]);
}
void func1(int rows, int cols, int *data)
{
ROWCOL(data, 5, 6) = 5; /* Do various things to data */
func2(rows, cols, data);
}
void func2(int rows, int cols, int *data)
{
ROWCOL(data, 2, 3) = 12; /* Do various other things to data */
} |
Request for Answer Clarification by
jimmyjrosu-ga
on
16 Feb 2004 07:34 PST
It may be easier just to post what i have so far(that i am testing): I
can get everything to change by following your excellent answer, but
well here just look:
#include <stdio.h>
#define WORD_SIZE 20
#define MAX_NUM_OF_WORDS 50
void firstchange(int, int, int *);
void secondchange(int, int, int *);
int main(void)
{
words[MAX_NUM_OF_WORDS][WORD_SIZE];
firestchange(MAX_NUM_OF_WORDS, WORD_SIZE, &words[0][0]);
printf("\n%d%d", words[0][0], words[0][1]);
return (0);
}
void firstchange(int maxnum, int maxword, int *wordss)
{
wordss[maxnum*0+0]=4;
wordss[maxnum*0+1]=9;
printf("%d %d\n", wordss[maxnum*0+0], wordss[maxnum*0+1]);
secondchange(maxnum, wordss, 0, 1);
printf("%d %d\n", wordss[maxnum*0+0], wordss[maxnum*0+1]);
}
void secondchange(int max, int maxw, int wordss)
{
SHOOOOOOT!!!!!!!!!!!!-just answered my own question, in the original
program, I had a global variable named words, and then was wanting to
output the value of wordss, but was refering to words, no more closely
related variables for me.
|