here is a small example for you.

Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct employee{
    char *name;
    int  code;
    float salary;
}*emp;


int howmany=0;

void show_employees(),free_memory();

int main(){
    char name[30];
    int i=0,epilogi;
    name[0]='1';
    printf("\nGive * for name to stop adding employees!");
    while (name[0]!='*'){
        i++;
        if (i==1) emp=(struct employee *) malloc(sizeof(struct employee));
        else
            emp=(struct employee *) realloc(emp,i*sizeof(struct employee));
        if (emp==0){
            printf("\nnot enough memory!!");
            name[0]='*';
        }
        else{
          printf ("\nEmployee No: %d\nGive name: ",i);
          scanf("%s",name);
          getchar();
        }
        if (name[0]!='*'){
          emp[i-1].name=(char *) malloc(strlen(name)+1);
          strcpy(emp[i-1].name,name);
          printf("Give code : ");
          scanf("%d",&emp[i-1].code);
          getchar();
          printf("Give salary : ");
          scanf("%f",&emp[i-1].salary);
          getchar();
          howmany=i;
        }
    }
show_employees();
free_memory();
            printf("end of program.\n");
            getchar();
            exit(0);
return 0;
}//end of main




void show_employees(){
    int i;
    for (i=0; i<howmany; i++)
    printf ("|%5d | name:%20s | code:%8d | salary:%10.2f |\n",i+1,emp[i].name,emp[i].code,emp[i].salary);
    
}

void free_memory(){
    int i;
    for (i=0;i<howmany;i++){//free memory pointers
    free(emp[i].name);
}
    free(emp);
}
askmewhy25 like this