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!

Coder123
  • 629
  • 1
  • 8
  • 24

3 Answers3

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);
    }
}
dbush
  • 162,826
  • 18
  • 167
  • 209
  • `&e[i].name` should be `e[i].name`. And those spaces before the format specifier are unnecessary. – Spikatrix Sep 02 '15 at 16:23
  • @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](http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf) – Hardik Modha Sep 02 '15 at 16:27
  • 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? – Coder123 Sep 02 '15 at 16:30
  • @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. – Spikatrix Sep 02 '15 at 16:35
  • @user3142625 What? Could you clarify? Your comment seems to be unclear for me. – Spikatrix Sep 02 '15 at 16:37
  • @CoolGuy Yes..We should avoid it. I was just making clear that we can use `&` also. (see my answer I haven't used it! ;) ) – Hardik Modha Sep 02 '15 at 16:39
  • hmm dbush wrote "Never fflush(stdin). It's undefined behavior." i do not understand why :( – Coder123 Sep 02 '15 at 16:55
  • @user3142625 The leading space in each `scanf` format string matches zero or more whitespace characters, so any newlines or spaces are skipped over. Also, from the `fflush` manpage: "The function fflush() forces a write of all user-space buffered data for the given *output or update stream* via the stream’s underlying write function." `stdin` is not an output or update stream. – dbush Sep 02 '15 at 17:00
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..

John Bollinger
  • 121,924
  • 8
  • 64
  • 118
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;
}
Hardik Modha
  • 9,434
  • 3
  • 31
  • 38