Hello i’m selfstudying C and i’m a bit confused about the following code since i don’t know if i’m understanding the code properly. I would be very thankful if someone could read my explanation and correct me if i’m wrong.
The code is from a header file. The function of the program should be uninteresting at this point, since my comprehension problem is about the pointers and the values the functions give back. So first of all i’m declaring 3 arrays of char and an integer in my employee struct.
struct employee
{
char firstname[11];
char lastname[11];
char number[11];
int salary;
}
5 functions are declared in the header file. The first function takes 4 values (3 pointers and one int) and gives back a pointer to a struct. The second function gets a pointer to the “struct employee” and gives back a pointer to an element of the array “char firstname” in the struct employee. The functions 3 and 4 are doing the same for the other both arrays.
The function 5 gets a pointer to the struct employee but gives back an int and not a pointer. So it is just using the declared variable in the struct.
struct employee* createEmployee(char*, char*, char*, int); //1
char* firstname (struct Employee*); //2
char* lastname (struct Employee*); //3
char* number (struct Employee*); //4
int salary (struct Employee*); //5
Your understanding is pretty much correct. To be a little more accurate and/or less abusive of the English language, functions 2-4 return a pointer to an element of the corresponding array.
The idea is that the contents of each array represent some kind of text, with each element corresponding a character of text, up until the first appearance of a zero value (used to mark the end of the “string”). Keep in mind that there is no provision for Unicode here, or even for specifying an encoding: we assume ASCII, and for any bytes not in the range 0..127, all bets are off. A better name for the type
charwould bebyte; but we didn’t really know any better back then. (You should also be aware thatcharis a separate type from bothsigned charandunsigned char, and thatcharmay or may not be signed.)This way, the names and number can be any length up to ten (an 11th byte is reserved in order to have room for the “null terminator” with the zero value), and the returned pointer can be used to inspect – and modify; but that might not be a good idea – the data in the arrays that are part of the
Employeestructure.The
intreturned as theEmployee‘ssalaryis a copy of the one in the struct. So although we can see the whole value, this does not let us modify the value in the struct. Of course, as long as we have thestructdefinition and anEmployeeinstance, there is no protection; we can access the member directly instead of going through the function.In C, we get “encapsulation” and “data hiding” by not providing these definitions. Instead, we would just put
struct Employee;and the function declarations in the header, and the struct definition in the implementation file. Now calling code doesn’t know anything about what anEmployeeis; only about what it can do.