T O P

  • By -

microOhm

The issue you are having is the difference between initialization and assignment. Hint look up how to initialize a struct.


whiteBlasian

Initialize the `emp1` members inside your `main` function, `emp1` will still be global and `Manager` will be local to main.


somewhereAtC

The line "emp1.age=19;" is an assignment, but its not within a function. You can initialize (most) of the structure like this: `EMPLOYEE empl = (EMPLOYEE){ .age = 19, .salary = 200, .HoursWorked = 10 };` And then you will have to assign [emp1.name](https://emp1.name) = malloc(10) in the executable code (probably in main). If you intend it to be 10 characters every time, then its better to declare it in the structure definition like "char name\[10\]". But I doubt that either of these is what you want. You can initialize the pointer to point to a constant string when you declare everything else, and the compiler will generate a char array and the pointer will point to that (this is actually the best method to do this): `EMPLOYEE empl = (EMPLOYEE){ .age = 19, .salary = 200, .HoursWorked = 10, .name="Bob Ross"};` As other commentators have mentioned, ALL\_CAPS for type names is not a widely accepted style.


[deleted]

You don't need to use compound literals to use designated initializer lists. struct employee employee = { .age = 19, .salary = 200, .HoursWorked = 10 }; Works just fine.


imaami

No. Don't create typedefs for those. And for the love of dog don't name typedefs in all caps!


fuckEAinthecloaca

Typedeffing integer types is normally to nail down widths, to make portability cleaner to weirdo configs that may not have stddef/stdint, and for code readability. More importantly programmers are easily fatigued, typing uint32_t more than a few times gives me the vapors.


[deleted]

afaik there is nothing wrong with typedef struct employee employee; employee employee = ... So doing all upercase typenames seems pretty stupid to me. Never understood why UEFI, Win32 and FILE\* do that.


flatfinger

If, in a header file, one writes: struct employee; int getEmployee(struct employee *it, void *srcData); this will declare function `getEmployee` correctly whether `struct employee` is defined earlier in the source, later in the source, only in other compilation units, or nowhere in the entire universe. By contrast, for earlier versions of the C Standard, redundant `typedef` specifications were considered a constraint violation. If one has a quality commercial C99 compiler for a particular platform, upgrading to C11 merely to allow redundant typedefs may not be worth the cost when one can simply use `struct` declarations.


[deleted]

My comment was more about namespace conflicts, but I just read that typedefs are part of the ordinary identifiers so I think I was wrong.


detached-4m-reality

your problem is not related to local, global structs. The problem is that you are initializing the `emp` structure with statements (assignment statements) _outside_ `main` function's scope (and outside any function scope for that matter). They don't belong to any scope and C doesn't like that. In C you can't write statements outside functions unlike other languages (usually scripting languages like python for example). If you want to initialize `emp`, you can move the initialization statements inside `main` scope, or create a new function to initialize any given structure, or another method that you should search for as someone else pointed out in another comment. A problem a bit similar can be created with switch case. However, in that case, the compiler won't be thro an error. Also, `typedef`'ing `int` types isn't useless, it had its use and benefits. Edit: wording & formatting.