-1

QUESTION: WAP to enter id, name, age and basic salary of n number of employees. Calculate the gross salary of all the employees and display it along with all other details in a tabular form, using pointer to structure.

    --->

#include <stdio.h>
#include <conio.h>
 
struct employee {
char name[100];
int id, age;
int salary;
};
 
int main(){
   struct employee emp, *ptr;
    
int i,n;
    printf("Enter the no of employees\n");
    scanf("%d",&n);
    printf("****************INPUT EMPLOYEE DETAILS******************\n");
    for(i=0;i<n;i++)
    {
    printf("\nEnter employee id of employee %d : ",i+1);
    scanf("%d", &emp.id);

    printf("Enter name of employee %d : ", i+1);
    scanf("%s", &emp.name);

    printf("Enter age of employee %d : ", i+1);
    scanf("%d", &emp.age);

    printf("Enter salary of employee %d : ", i+1);
    scanf("%d", &emp.salary);
        
    }
printf("\n");
    printf("                   DISPLAYING EMPLOYEE DETAILS                       \n");
    printf("*********************************************************************\n");

   ptr = &emp;

  printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");

  for(i=0;i<n;i++){
   printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
  }
   return 0;
}
  • You have a pointer `ptr` which you make point to ***the single variable*** `emp`. It doesn't matter that you read input `n` times, you still only have one single `employee` structure. Please go back to your beginners book and read about *arrays*. – Some programmer dude Jul 28 '21 at 05:12
  • Also note that `&emp.name` as arguments for `scanf("%s")` is wrong. The `%s` format expects an argument of type `char *`. The type of `&emp.name` is `char (*)[100]`. Mismatching format specifier and argument type leads to *undefined behavior*. What you should pass is plain `emp.name` (or `&emp.name[0]` which is exactly the same thing as `emp.name`). – Some programmer dude Jul 28 '21 at 05:14

1 Answers1

0

First of all you need to save each of your employees in variable so you can refer them later. Second because your variable is structure and you want loop through it your best choice is to use array. I changed your code and highlighted them it worked for me i hope it will help you.

#include <stdio.h>
 #include <conio.h>
 
  struct employee {
  char name[100];
  int id, age;
  int salary;
 };
 
int main(){
   struct employee *ptr; //One change is here
    
int i,n;
    printf("Enter the no of employees\n");
    scanf("%d",&n);
    struct employee emp[n];
    printf("****************INPUT EMPLOYEE DETAILS******************\n");
    for(i=0;i<n;i++)
    {
    printf("\nEnter employee id of employee %d : ",i+1);
    scanf("%d", &emp[i].id);

    printf("Enter name of employee %d : ", i+1);
    scanf("%s", emp[i].name); // Another change is here "%s" doesn't need '&'

    printf("Enter age of employee %d : ", i+1);
    scanf("%d", &emp[i].age);

    printf("Enter salary of employee %d : ", i+1);
    scanf("%d", &emp[i].salary); 
        
    }
printf("\n");
    printf("                   DISPLAYING EMPLOYEE DETAILS                       \n");
    printf("*********************************************************************\n");

  printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");

  for(i=0;i<n;i++){
    ptr = &emp[i];   // This yet another change i grab "ptr" down here and assign it to "i" in each loop
   printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
  }
   return 0;
}
javad
  • 11
  • 1
  • 1
  • 3
  • So why it's working properly on my computer and has the intended result? – javad Jul 28 '21 at 06:32
  • You might want to read a bit about *undefined behaviour*. The very basic nature of this is that it can do anything. It is even possible to look as if it worked while in fact it does not. That is why it is called **undefined** in first place. The first beginners lessons should teach you that local variables are not initialized and have indetermined value. Accessing this is not allowed. What if `m` by accident contains `INT_MAX`? You will probably cause stack overflow with a huge array. If `m` happens to be less than `n` you will read values into memory that does not belong to you. – Gerhardh Jul 28 '21 at 06:35
  • You might scan values into memory area that is used for `i` or `n` causing weird effects. – Gerhardh Jul 28 '21 at 06:37
  • And finally: "Works on my computer" **NEVER** is a good starting point for any discussion. – Gerhardh Jul 28 '21 at 06:38