Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
What is the difference between these two structure declarations?

I am confused about these two structures from different tutorials:

typedef struct complex {
    float real;
    float imag;
} COMPLEX;

typedef struct {
    float real;
    float imag;
} COMPLEX;

COMPLEX c1;

Are they both correct? Why? and is it necessary to add the lowercase of complex before struct? What is general situation?

over 4 years ago · Santiago Trujillo
7 answers
Answer question

0

With the first you can use either the type-alias COMPLEX or struct complex.

With the second you have an anonymous structure which can only be used with the type-alias COMPLEX.

With that said, in C++ any structure name is also a type-name and can be used as a type directly:

struct complex { ... };
complex c1;
over 4 years ago · Santiago Trujillo Report

0

typedef struct complex{
    float  real;
    float imag;
}COMPLEX;

This is a struct complex which has been typedef'ed to COMPLEX. You can now use both struct complex c1; and COMPLEX c1;. As you tagged this question C++, you can also use complex c1; as the struct (and thus, the typedef to avoid it) is not necessary in C++.

typedef struct {
    float  real;
    float imag;
}COMPLEX;

This is an unnamed struct, typedef'ed to COMPLEX. You can now use COMPLEX c1;.

So far for the difference between these two (C) constructs.

As you tagged this question C++, the correct thing to do would be to use the features of the C++ header <complex>:

std::complex< float > c1;
over 4 years ago · Santiago Trujillo Report

0

This is really because the type system changed subtly between C and C++. User defined structures (and enums) in C had "tags", which were treated as different from types.

struct Tag {/*some members*/};
typedef struct Tag Type; /* Tag and Type can be the same name */

This way of working is still supported as your example shows, but C++ added a new rule, that says that structure tags are always types as well. Thus, viewed from a C view-point it's as if the C++ compiler adds the typedef for you. Thus in C++ you no longer need to say "struct" before declaring variable of your structure type.

So yes, both ways are "correct", one is the old C way, and will work in both C and C++, the other is the native C++ way, and will not compile in C. It's possible your example needed C compatibility for some of the code but not all.

If you need C-compatibility you should use the pattern above, but it is not common practice to use it for native C++ code.

over 4 years ago · Santiago Trujillo Report

0

struct definition is as below:

struct type_name {
       member_type1 member_name1;
       member_type2 member_name2;
       .
       .
                } object_names;

you use one name for struct name and object name. you can use object in side of structure definition without declare a name for struct

over 4 years ago · Santiago Trujillo Report

0

Think of "namespaces" here: struct names live in their own namespace.

struct complex {
    float real;
    float imag;
};

struct complex myvar; // in C and C++
complex myvar // only in C++!

C++ also resolves struct-tags (if not otherwise possible), where C does not. typedef defines an alias for a typename, i.e.

typedef struct complex COMPLEX;

will define a new name COMPLEX which is a stand-in for struct complex.

This declaration

typedef struct complex {
    float real;
    float imag;
} COMPLEX;

is shorthand for

struct complex {
    float real;
    float imag;
};
typedef struct complex COMPLEX;

You can also omit the struct-tag altogether

typedef struct {
    float real;
    float imag;
} COMPLEX;

just as in

struct {
    float real;
    float imag;
} myvar;

which defines a variable myvar with an unnamed struct type.

over 4 years ago · Santiago Trujillo Report

0

There is nothing like C/C++ language.

In C.

   typedef struct complex {
        float real;
        float imag;
    } COMPLEX;

    COMPLEX c1;
    struct complex c2;

Aliases struct complex to type COMPLEX

   typedef struct{
        float real;
        float imag;
    } COMPLEX;

    COMPLEX c1;

Aliases tagless structure to type COMPLEX

What is the practical difference?

As you see using the first one you can define new objects using the new type COMPLEX and struct complex.

The secon type decvlaration allows only definitions using COMPLEX type.

over 4 years ago · Santiago Trujillo Report

0

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:

struct Foo { ... };
Foo x;

you would get a compiler error, because Foo is only defined in the tag namespace.

You'd have to declare it as:

struct Foo x;

Any time you want to refer to a Foo, you'd always have to call it a struct Foo. This gets annoying fast, so you can add a typedef:

struct Foo { ... };
typedef struct Foo Foo;

Now struct Foo (in the tag namespace) and just plain Foo (in the ordinary identifier namespace) both refer to the same thing, and you can freely declare objects of type Foo without the struct keyword.

The construct:

typedef struct Foo { ... } Foo;

is just an abbreviation for the declaration and typedef.

Finally,

typedef struct { ... } Foo;

declares an anonymous structure and creates a typedef for it. Thus, with this construct, it doesn't have a name in the tag namespace, only a name in the typedef namespace. This means it also cannot be forward-declared. If you want to make a forward declaration, you have to give it a name in the tag namespace.

In C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!