0

I was trying to pass one of the 20 "database" structs I made

here is my prototype for the function "add"

void add(struct database test);

I want to pass my database struct and for now i'll just call it "test"

Here is my database structure

struct database
{
    char ID[6];
    char Duration[3];
};

main()
{

   char menu;
   struct database employee[20]; // I make 20 employee variables
   int a = 0;                    /*A counter I use to edit certain structs 
                                   ie a=2  "employee[a]" = "employee[2]" */

I then call the function like this:

add(employee[a]);
a++;  /*Once it exits this I want it to go to the next employee
       variable so I increment the counter */

The actual function looks like this:

void add(struct database test)
{
    static int a = 0;

    printf("\nPlease enter a 5 digit employee number: ");
    scanf("%s", test[a].ID);

    a++
}

while doing this I get the error:

Error E2094 Assignment.c 64: 'operator+' not implemented in type 'database' for arguments of type 'int' in function add(database)

It says the error is at

scanf("%s", test[a].ID);

Thanks in advance for any help, and I apolgise if I have formatted this wrong, still learning for to use stack overflow, so sorry!

1
  • why cant you pass struct pointer as void add(struct database *test);, change add(employee[a]); to add(&employee[a]); and scanf("%s", test[a].ID); to scanf("%s", test->ID); Commented Apr 14, 2014 at 12:52

2 Answers 2

2

This is what you need to do in order to get it right:

void add(struct database* test)
{
    printf("\nPlease enter a 5 digit employee number: ");
    scanf("%s",test->ID);
}

int main()
{
    ...
    int a;
    struct database employee[20];
    for (a=0; a<sizeof(employee)/sizeof(*employee); a++)
        add(&employee[a]); // or add(employee+a);
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

For the next person who see this: Make note of the & in the call to add(). Easy to miss.
1

add(struct database test) declares a struct database as parameter. This is not an array, so you cannot index it.

So

test[a]

is invalid.


Also the int a inside add() is different from the int a defined in main(). Inside add() the latter a is hidden by the former a.


Also^2 you are passing to add() a copy of the array's element declared in main(). So any modifications done to test in side add() are lost when returning from add(). They won't be visible in the array declated in main().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.