Difference between ‘struct’ and ‘typedef struct’ in C++?

In C++, there is only a subtle difference. It’s a holdover from C, in which it makes a difference. The C language standard (C89 §3.1.2.3, C99 §6.2.3, and C11 §6.2.3) mandates separate namespaces for different categories of identifiers, including tag identifiers (for struct/union/enum) and ordinary identifiers (for typedef and other identifiers). If you just said: … Read more

C++ Expression must have pointer-to-object type

You probably meant: since c_info is an array, by doing c_info[i] you’ll access the i-th instance (object) of Employee class in c_info array, and then obtain hoursWorked through . operator. Now you can clearly see that your variant simply doesn’t make sense, as hoursWorked is just an integral type and not an array, and therefore … Read more

How to properly malloc for array of struct in C

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with: In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc(). Note that’s it’s better style to use: … Read more

size of struct in C

The compiler may add padding for alignment requirements. Note that this applies not only to padding between the fields of a struct, but also may apply to the end of the struct (so that arrays of the structure type will have each element properly aligned). For example: Even though the c field doesn’t need padding, the struct … Read more

What are the differences between struct and class in C++?

You forget the tricky 2nd difference between classes and structs. Quoth the standard (§11.2.2 in C++98 through C++11): In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class. And just for completeness’ sake, the more widely known difference between class and … Read more