
Aarghh! I need help with array of structs
Declare it as follows:
typedef struct {
char word[25];
int page;
int length;
Quote:
} index[100];
or you could also declare it as:
struct entry {
char word[25];
int page;
int length;
Quote:
};
typedef struct entry index[100];
As you can obviously see the former is more comprehensive.
And in order to send the structure to a function as an argument, you'll have
to send the address to the function and store it in a pointer.
i.e.
index idx1;
function(&idx1);
and the function would be like:
function(index *idx) {
for (int i=0;i<100li++) {
printf("Word: %s",idx[i]->word);
//idx[i]->word OR (*idx[i]).word
printf("Page: %d",idx[i]->page);
// -DO-
printf("Length: %d",idx[i]->length);
// -DO-
Quote:
}
or you could try this:
index idx1,*idx;
idx=&idx1;
function(idx);
and the function would be like:
function(index *idx) {
for (int i=0;i<100li++) {
printf("Word: %s",idx[i]->word);
//idx[i]->word OR (*idx[i]).word
printf("Page: %d",idx[i]->page);
// -DO-
printf("Length: %d",idx[i]->length);
// -DO-
Quote:
}
-CSK
http://snap.to/csk
Quote:
> I have to write a program in C(not C++) that uses an array of structs.
> the array of structs must be declared as a typedef.
> I Put this :
> struct entry{
> char word[25];
> int page;
> int length;
> };
> typedef entry index[100];
> above the main in my program, right befor the function prototypes.
> can anybody tell me if this is correct?
> I also dont know how to send either a single entry struct or an entire
> index array to a function(by address, I must alter them).
> can someone tell me what the prototype, function header and actual
> function call should look like??
> if you so desire to see the entire program it is at:
> http://members.tripod.com/amattera/cprog.htm
> Any help would be greatly appreciated
> Sent via Deja.com http://www.deja.com/
> Before you buy.