Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:59:57+00:00 2026-05-18T02:59:57+00:00

gcc 4.4.4 c89 I have always done the following when using structs to hide

  • 0
gcc 4.4.4 c89

I have always done the following when using structs to hide the elements in the implementation file.

port.h header file

struct port_tag;
struct port_tag* open_ports(size_t port_id);
void close_ports(struct port_tag *port);

port.c implementation file

#include "port.h"
typedef struct port_tag {
    size_t port_id;
} port_t;

port_t* open_ports(size_t port_id)
{
    port_t *port = malloc(sizeof *port);
    if(port == NULL) {
        return NULL;
    }
    port->port_id = port_id;
    return port;
}

void close_ports(port_t *port)
{
    if(port != NULL) {
        free(port);
    }
}

driver.c driver file

#include "port.h"
int main(void)
{
    size_t i = 0;
    struct port_tag *port = NULL;

    for(i = 0; i < 5; i++) {
        port = open_ports(i);

        if(port == NULL) {
            fprintf(stderr, "Port [ %d ] failed to open\n", i);
        }
        close_ports(port);
    }
    return 0;
}

In the above code it is clear that the tag name is port_tag and the actual typedef’ed name is port_t.

However, I am re-engineering some one code. And I have found they have used a different method, that I have never seen before. I have a few questions about their method.

channel.h header file

typedef struct channel_t channel_t;
channel_t* open_channels(size_t channel_id);
void close_channels(channel_t *channel);

channel.c implementation file

#include "channel.h"
struct channel_t {
    size_t channel_id;
};

channel_t* open_channels(size_t channel_id)
{
    channel_t *channel = malloc(sizeof *channel);

    if(channel == NULL) {
        return NULL;
    }
    channel->channel_id = channel_id;
    return channel;
}

void close_channels(channel_t *channel)
{
    if(channel != NULL) {
        free(channel);
    }
}

driver.c driver file

#include "channel.h"   
int main(void)
{
    size_t i = 0;
    channel_t *channel = NULL;

    for(i = 0; i < 5; i++) {
        channel = open_channels(i);

        if(channel == NULL) {
            fprintf(stderr, "Channel [ %zu ] failed to open\n", i);
        }

        close_channels(channel);
    }
    return 0;
}

1) When they have declared the typedef’ed struct which is the tag name or the name of the
struct itself?

typedef struct channel_t channel_t;

2) In the implementation file shouldn’t the name of the struct follow the last curly brace?

struct channel_t <-- tag name {
    size_t channel_id;
} <-- itsn't this the name of the typedef'ed struct;

Many thanks for any advice,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:59:58+00:00Added an answer on May 18, 2026 at 2:59 am

    1. The struct type is struct channel_t, and the new typedef is channel_t.

    That means it can now be used as:

    channel_t some_instance;
    

    2. They’re creating a typedef elsewhere, not here. So all:

    struct channel_t {
        size_t channel_id;
    };
    

    does is define a struct type with tag channel_t. No typedef is required when defining a struct. It can be used as, e.g.:

    struct channel_t some_instance;
    

    As you’ve seen, the two syntaxes are basically equivalent in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.4 c89 I have the following file that contains name, age, and gender.
gcc 4.4.2 c89 I have these defines in our API header file. /* Reason
gcc 4.4.4 c89 I have always thought of using malloc for the life of
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
gcc 4.4.2 c89 I have the following code. #if defined ( __linux__ ) log_msg(stderr,
gcc 4.4.2 c89 I have a file called main.c. I want the result of
gcc 4.4.2 c89 I have a wave file: 8000 Hz 16 bit I am
gcc 4.4.4 c89 I have the following function but I cannot free the memory.
gcc 4.4.3 c89 linux I am using log4c and have placed the inc and
Using gcc 4.4.3 c89: I am just working on the following code below: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.