Aarghh! I need help with array of structs 
Author Message
 Aarghh! I need help with array of structs

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://www.*-*-*.com/
Any help would be greatly appreciated

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 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.



Fri, 23 Aug 2002 03:00:00 GMT  
 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);
                                printf("Page: %d",idx[i]->page);
                                printf("Length: %d",idx[i]->length);

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);
                                printf("Page: %d",idx[i]->page);
                                printf("Length: %d",idx[i]->length);

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.



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

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?

Ask your compiler...

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

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?

It is almost correct. The typedef should be:typedef struct entry index[100];

Quote:
> 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??

#include <stdio.h>
#include <string.h>

#define MAX 100

struct entry{
   char word[25];
   int page;
   int length;
   };

typedef struct entry index[MAX];

void add_entry(index *p,size_t *n) {
   if(*n >= MAX) {
      puts("Entry array full");
      return;
      }
   /* TODO code to get input for entry */
   strcpy(p[*n]->word,"Example"); /* As an example */
   p[*n]->page = 100;
   p[*n]->length = 7;
   (*n)++;
   return;
   }

int main(void) {
   index dict;
   size_t i,count = 0;

   add_entry(&dict,&count);
   for(i = 0;i < count;i++)
      printf("Word: %-25sPage: %-10dLenth: %d\n",
                dict[i].word,dict[i].page,dict[i].length);
   return 0;
   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

Quote:


> > can anybody tell me if this is correct?

> It is almost correct. The typedef should be:typedef
> struct entry index[100];

I'd query with your professor whether the struct or the
entire array must be typedefed.

Though Al's answer was correct, in general typedefing an
array is not something you would want to do in a real
program (because it is too confusing for the reader). Also
in a real program you would probably want to #define
MAX_ENTRIES 100 if you wanted to be able to change the
array size flexibly.

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs
Quote:

> #define MAX 100

> struct entry{
>    char word[25];
>    int page;
>    int length;
>    };

> typedef struct entry index[MAX];

> void add_entry(index *p,size_t *n) {
>    if(*n >= MAX) {
>       puts("Entry array full");
>       return;
>       }
>    /* TODO code to get input for entry */
>    strcpy(p[*n]->word,"Example"); /* As an example */
>    p[*n]->page = 100;
>    p[*n]->length = 7;
>    (*n)++;
>    return;
>    }

This code loses. Note that p is not a pointer to struct entry, but a
pointer to an array[MAX] of struct entry. This means that, when
add_entry is called a second time (*n == 1), p[*n] addresses the
non-existent second array[MAX] in a non-existent array[] of array[MAX].

Quote:

> int main(void) {
>    index dict;
>    size_t i,count = 0;

>    add_entry(&dict,&count);

[...]

Eddy



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

Quote:


> > #define MAX 100

> > struct entry{
> >    char word[25];
> >    int page;
> >    int length;
> >    };

> > typedef struct entry index[MAX];

> > void add_entry(index *p,size_t *n) {
> >    if(*n >= MAX) {
> >       puts("Entry array full");
> >       return;
> >       }
> >    /* TODO code to get input for entry */
> >    strcpy(p[*n]->word,"Example"); /* As an example */
> >    p[*n]->page = 100;
> >    p[*n]->length = 7;
> >    (*n)++;
> >    return;
> >    }
> This code loses. Note that p is not a pointer to struct entry, but a
> pointer to an array[MAX] of struct entry. This means that, when
> add_entry is called a second time (*n == 1), p[*n] addresses the
> non-existent second array[MAX] in a non-existent array[] of array[MAX].

Oops, yes, thanks for pointing out the flaw.

function add entry should be prototyped.:
void add_entry(index p, size_t *n);
And in function definition of add_entry change the "->" operators to "."
operators.
And in function main, the call to: add_entry(dict, &count);

There is another problem which has occurred when I compiled with gcc.
There is a conflict of symbol 'index' with code in string.h.
Is 'index' reserved?

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Fri, 23 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

...
Quote:
> There is another problem which has occurred when I compiled with gcc.
> There is a conflict of symbol 'index' with code in string.h.
> Is 'index' reserved?

Not in Standard C, which you can get from gcc (at least with glibc)
by telling the compiler -ansi.  I believe it was in BSD, and that's why
GNU-extended C has it.

--
- David.Thompson 1 now at worldnet.att.net



Sat, 31 Aug 2002 03:00:00 GMT  
 Aarghh! I need help with array of structs

Quote:


> ...
> > There is another problem which has occurred when I compiled with gcc.
> > There is a conflict of symbol 'index' with code in string.h.
> > Is 'index' reserved?

> Not in Standard C, which you can get from gcc (at least with glibc)
> by telling the compiler -ansi.  I believe it was in BSD, and that's why
> GNU-extended C has it.

Actually the GNU C compiler doesn't know anything about index().
The GNU C library does indeed have it, but you don't have to use
the GNU C compiler with the GNU C library.

[Followups set.]



Sat, 31 Aug 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. arrays of structs - information needed

2. Need 2 access struct array from a .dll

3. Problem with struct/array inside a struct using fread/fwrite

4. struct.array works - struct->pointer not

5. Initialisation of structs containing arrays of structs...?

6. native dll calling with structs containing arrays of nested structs

7. Help: Pointer to 2-D array of structs.

8. Help with an Array of struct

9. help me sort my array of structs

10. Pointer to a struct within an array Help Please

11. Help wanted with array of pointers to structs

12. help with array of structs

 

 
Powered by phpBB® Forum Software