Can You Type Alias an Existing User-Defined Type in C?

Say I want to define two structs, the first one has a type, and the other I want to be of the same type as the first. Is this possible in C99?

Example:

typedef struct {
    fieldA;
    fieldB;
} typeA;


struct B {
    fieldA
    fieldB
};

typedef B A;

If not typedef, is there another keyword/way to do this?

Edit: It seems I wasn't being as clear as possible, judging from the feedback so far (which I greatly appreciate!)

My specific scenario is a typedef struct definition that is required to type alias another struct.

4

1 Answer

Yes of course! It's just like:

#include <stdio.h>

typedef int value_t;
typedef value_t data_t;

int main()
{
    data_t i = 1;
    printf("%d\n", i);

    return 0;
}

You can typedef as many times as you like.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest