I would like learn how to pass, by reference, an array of structs to the second function called/executed from within the first function. My goal is to modify/change the contents of arbitrary struct from the second function only. The code below works, but, unfortunately, does not do exactly what I want to achieve. I would to have access to arbitrary struct within second function. In other words, I would like to process all structs (using for loop) within second function by calling/executing first function in main only once and not using for loop.
The second function, in the code below, is named passByReference_inner.
array_of_struct.h :
struct card
{
int face;
int nose;
};
typedef struct card HEAD ;
/* prototype */
extern void passByReference(HEAD **c); /* first function */
extern void passByReference_inner(HEAD *c); /* second function */
first function: (passByReference)
#include <stdio.h>
#include "array_of_struct.h"
void passByReference(HEAD **c)
{
passByReference_inner (*c); /* second function */
}
second function: (passByReference_inner)
#include <stdio.h>
#include "array_of_struct.h"
void passByReference_inner(HEAD *c)
{
c->face = (c->face) + 1000;
c->nose = (c->nose) + 2000;
}
main:
#include <stdio.h>
#include "array_of_struct.h"
int main(void)
{
int i;
static HEAD c[12];
static HEAD *cptr[12];
for ( i = 0; i < 12; i++ )
{
c[i].face = i + 30;
c[i].nose = i + 60;
cptr[i] = &c[i];
}
for ( i = 0; i < 12; i++ )
{
passByReference(&cptr[i]); /* first function */
}
return 0;
}