Amiga.org

Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: TheMagicM on January 12, 2005, 05:02:02 AM

Title: GAH!!!! Help w/passing array of structure to a function...
Post by: TheMagicM on January 12, 2005, 05:02:02 AM
example...


struct tele2_struct{
   fname[30];
   lname[30];
   whatever[70];
}db[750];


int search_array(struct tele2_struct, char parm_searchvar);

then in main() I have something similiar like...

found_record = search_array(db, searchvar);

I basically want to pass a array which contains various info and what they are searching for which is stored in searchvar.. but I get a error @ found_record which says...

 conversion from `tele2_struct*' to non-scalar type `
   tele2_struct' requested


and I'm lookin at examples here and there..but I cannot find one similiar to mine... stumped by something simple which I cannot figure out.
Title: Re: GAH!!!! Help w/passing array of structure to a function...
Post by: Lando on January 12, 2005, 05:08:08 AM
Try found_record = search_array(db[0], searchvar);

But, you should really pass a pointer to the structure rather than trying to pass the structure itself.
Title: Re: GAH!!!! Help w/passing array of structure to a function...
Post by: Trev on January 12, 2005, 07:06:03 AM
Expanding on Lando's suggestion. . . .

It sounds like you want to do something like this:
Code: [Select]

struct tele2_struct {
    char fname[30];    /* gotta specify a type   */
    char lname[30];    /* using char for example */
    char whatever[70];
} db[750];

int search_array(struct tele2_struct* ptele2, unsigned int psize, char parm_searchvar);

int main()
{
    char searchvar = 0;

    int res = search_array(db, sizeof(db), searchvar);
}

int search_array(struct tele2_struct* ptele2, unsigned int psize, char parm_searchvar)
{
    int res = 0;
    unsigned int i;
    struct tele2_struct* p = ptele2;

    for (i = 0; i < psize; i++) {
        /* do something with p->fname[], p->lname[], etc. */
        /* place the result in `res' */
        p++;
    }

    return res;
}

You can play with passing pointers, pointers to pointers, and references. Your final decision on what to use should be based partially on your requirements and partially on your coding style. . . .

Trev
Title: Re: GAH!!!! Help w/passing array of structure to a function...
Post by: TheMagicM on January 12, 2005, 12:29:30 PM
LOL...yep...that'd be it..  thanks!