0

i've created a struct "Employee"

#define MAX_SIZE 20

typedef struct Employee{
    char name[MAX_SIZE];
    int s;
    int e;
} employee_s;

and i need to create an array of 2 employees and ask the user to initialize them, nothing i try seem to work,

void main()
{
    int i, s, e;
    char name[10];
    Employee* e[3];

    *e = (Employee*)malloc(sizeof(Employee)*ARR_SIZE);

    for(i=0; i < 3; i++)
    {
        fflush(stdin);
        puts("Please enter Employee's name:");
        scanf("%c",&name);
        *e[i]->name = name;

        puts("Please enter Employee's salary:");
        scanf("%d",&s);
       *e[i]->s= s;

        puts("Please enter Employee's experience:");
        scanf("%d",&e);
       *e[i]->e=e;

    }
}

p.s: i dont have to use dynamic allocation,
what do i do wrong?

thank you!

3 Answers 3

4

There are several errors here:

  • Employee is not a valid type. struct Employee is, and so is employee_s.
  • e is defined in multiple places
  • When reading in name, use %s (for a string), not %c (for a char)
  • Your employee array is defined as an array of pointers. That's probably not what you want. You just need an array. No call to malloc either.
  • Never fflush(stdin). It's undefined behavior.
  • In your scanf calls, put a space as the first character in the string. That will allow any newlines to be passed over.

The result:

int main()
{
    int i;
    employee_s e[3];

    for(i=0; i < 3; i++)
    {
        puts("Please enter Employee's name:");
        scanf(" %s",&e[i].name);

        puts("Please enter Employee's salary:");
        scanf(" %d",&e[i].s);

        puts("Please enter Employee's experience:");
        scanf(" %d",&e[i].e);
    }

    for(i=0; i < 3; i++) {
        printf("emp %d: name=%s, sal=%d, exp=%d\n", i, e[i].name, e[i].s, e[i].e);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

&e[i].name should be e[i].name. And those spaces before the format specifier are unnecessary.
@CoolGuy Although generally we don't use & while reading in character array but I think there is not any problem in using &e[i].name instead of e[i].name. Take a look at this question
thank you very much! one thing i did not understand is why not using fflush(stdin), if i want to recive string it will not take the garbage values in the buffer otherwise?
@HardikModha Hmm. But as that answer you linked says, it is wrong and the standard doesn't say anything about it. Also, you get a warning. So it is better to avoid it.
@user3142625 What? Could you clarify? Your comment seems to be unclear for me.
|
2

You've got your declaration backward. This:

typedef struct Employee{
    char name[MAX_SIZE];
    int s;
    int e;
} employee_s;

declares a type named employee_s to be equivalent to struct Employee, and furthermore declares struct Employee. You appear to want this, instead:

typedef struct employee_s {
    char name[MAX_SIZE];
    int s;
    int e;
} Employee;

In this case you can omit employee_s from that if you wish; perhaps that would be less confusing.

Moreover, you are going about your allocation in a very strange way, especially since you don't require dynamic allocation. Why not just do this:

Employee e[3];

? Then you can (and should) skip malloc() altogether. You will then refer to the members of the array elements via the form e[0].name, etc..

Comments

1

You can do this easily without dynamic memory allocation as follows.

#include <stdio.h>
#define MAX_SIZE 20
typedef struct Employee{
    char name[MAX_SIZE];
    int s;
    int e;
} employee_alias;
int main(void) {
    int i;
    employee_alias e[3];
    for(i=0; i < 3; i++)
    {
        puts("Please enter Employee's name:");
        scanf("%s",e[i].name);
        puts("Please enter Employee's salary:");
        scanf("%d",&e[i].s);
        puts("Please enter Employee's experience:");
        scanf("%d",&e[i].e);

        printf("Entered Data\n");
        printf("Name : %s\n",e[i].name);
        printf("Salary : %d\n",e[i].s);
        printf("Experience : %d\n",e[i].e);
    }
    return 0;
}

1 Comment

char name[10]; is unused. So, Remove it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.