Tuesday, July 14, 2009

What does this mean line by line in C++?

struct employee *allocemp(const char *name,const char *id,int age,double salary){


struct employee


*emp=new struct employee;


emp-%26gt;name=strdup(name);


strcpy(emp-%26gt;id,id);


emp-%26gt;age=age;


emp-%26gt;salary=salary;


return emp;


}

What does this mean line by line in C++?
Function definition "allocemp" ... likely meant as short-hand for allocate employee.





Semi-Line-by-line :





(1) ALLOCEMP is called with two c-string arguments, NAME and ID, an integral AGE, and a double floating point SALARY.





(2) Space for a new employee struct is allocated from the heap and a pointer stored in the local variable EMP (an employee struct pointer).





(3) Member variables of the newly allocated employee are then assigned per the data sent as arguments to this function ... c-strings are duplicated, as they are reference types.





(4) The value in EMP (a pointer to the new employee struct) is returned to the calling code.








So basically it's just allocating and initializing a new employee structure.


No comments:

Post a Comment