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

  • Home
  • SEARCH
  • 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 3997774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:26:34+00:00 2026-05-20T07:26:34+00:00

In order to define a new datatype in C for example a type for

  • 0

In order to define a new datatype in C for example a type for linked list
one can use one of the following definitions

struct list_node {
    int x;
    struct list_node * next;
};

//1
typedef struct list_node list1;
//2
typedef struct list_node *list2;

From what I have seen the common practice is the first definition.

The question is if the second definition is also an acceptable practice.
In which cases if any should one prefer the second over the first?
Provided that the variables that we use are pointers to struct list_node
is it possible to do the same operations with both types?
What are the advantages of the first?

  • 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-20T07:26:35+00:00Added an answer on May 20, 2026 at 7:26 am

    I would require API users to type the “*”, i.e. typedef the struct, not a pointer to the struct. This is the widely-used style in GLib, which is one of the more popular C stacks.

    It works out well because it is important to know whether you have a pointer to a struct or the struct itself. For example, can you store NULL in a variable of the type? If you hide that an object is a pointer, you have to create a special NIL_NODE or other value to replace NULL. If you just make it a pointer, then people can treat it like one.

    Another important benefit is the ability to put the actual struct definition somewhere private, i.e. in the .c file instead of in the header. You can put just the typedef in the header, while keeping the struct itself opaque to API users. This requires people to only use the exported methods that you provide to manipulate the struct. It also means you don’t have to rebuild the world if you change the struct fields.

    Yet another reason to take this path is that sometimes you do want a struct that can be copied, and you still want to typedef it. Take a thing like GdkPoint:

      typedef struct {
         int x, y;
      } GdkPoint;
    

    Here it’s useful to allow direct struct access, like:

     GdkPoint point = { 10, 10 };
     GdkPoint copy = point;
     do_stuff_with_point(&copy);
    

    However, if your convention is that the “GdkPoint” typedef would be a pointer, you’re going to have to be inconsistent.

    Code is just clearer if you can tell what’s a pointer and what isn’t, and code is better encapsulated if you don’t put struct definitions in the header (for structs that represent abstract, opaque data types, which is maybe the most common kind).

    My default template for a C data type is something like this in the header:

    typedef struct MyType MyType;
     MyType* my_type_new(void);
     void    my_type_unref(MyType *t);
     void    my_type_ref(MyType *t);
    

    Then the actual “struct MyType” in the .c file, along with the new, the unref, and any operations on the type.

    The exception would be for types such as Point, Rectangle, Color where it’s “plain old data” and direct field access is desired; another variation is to have a my_type_free() instead of _unref() if you don’t need refcounting.

    Anyhow, this is one style, basically the GLib style, that’s widely-used and is known to work well.

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

Sidebar

Related Questions

I have the following files: view.jsp <@ page import=... <bean:define id=mForm name=myForm type=MyForm/> <html:form
In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary
I have a jQuery script: $.ajax({ url: /scripts/secure/development/ajax.asp, type: POST, dataType: text, data: cmd=addresses,
So I'm new to rails and am actually following a video tutorial from Lynda.com
I would like to define Sorted to be of type ErrorProviderMessageCollection , which is
In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses
I have some questions regarding ScalaTest: How can I ensure test-execution ORDER with ScalaTest,
I want to define a macro that can be invoked in different places (at
In order to perform a case-sensitive search/replace on a table in a SQL Server
In order to know how many times a pattern exists in current buffer, 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.