Originally Posted by
KidMan
What if a want a function to edit an array of structures???
I thought I answered that. You pass it like an array of any other type. It works the same.
Code:
#include <stdio.h>
struct foo {
int num1;
};
void setNum(struct foo obj1) { // By value
obj1.num1 = 10;
}
void setNum(struct foo *obj1) { // By address
obj1->num1 = 10;
}
void setNum(struct foo obj[], int size) { // By array (which is also by address)
int i;
for (i = 0; i < size; i++)
obj[i].num1 = i;
}
int main(void) {
struct foo obj[2];
obj[0].num1 = 5;
obj[1].num1 = 7;
printf("obj[0].num1 = %d\n", obj[0].num1);
setNum(obj[0]); // The first function
printf("obj[0].num1 = %d\n", obj[0].num1);
setNum(&obj[0]); // The overloaded function for pointers
printf("obj[0].num1 = %d\n", obj[0].num1);
setNum(obj, 2); // The overloaded function for arrays
printf("obj[0].num1 = %d\nobj[1].num1 = %d\n", obj[0].num1, obj[1].num1);
return 0;
}
/* Output:
obj[0].num1 = 5
obj[0].num1 = 5
obj[0].num1 = 10
obj[0].num1 = 0
obj[1].num1 = 1 */