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 9140917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:34:44+00:00 2026-06-17T09:34:44+00:00

recently I found typedef in my code works really different than what I proposed.

  • 0

recently I found typedef in my code works really different than what I proposed. An example like this:

typedef int *ptype;
ptype v1 = 0, v2 = 0;

The result: both v1 and v2 were defined as a pointer to int type. But if you simply replace ptype by int * in the second sentence as int *v1 = 0, v2 = 0; or int* v1 = 0, v2 =0;, only v1 will be the pointer and v2 is normal int. It seems typedef does not do a simple replacement. What’s more, when it comes to complicate modifier like:

typedef int *ptype;
const ptype v3 = 0;

The result will be: v3 is a const pointer, not a pointer to the const int if we write const int *v3 = 0;. In the above code const stands as the modifier to the whole ptype, not the int inside ptype. Thus it really looks like typedef combines the compound type int* and creates a new type.

However, the authoritative C/C++ reference website cplusplus says “typedef does not create different types. It only creates synonyms of existing types.” so I was really confused and hope someone can help explain the behavior of typedef. Thanks!

  • 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-06-17T09:34:45+00:00Added an answer on June 17, 2026 at 9:34 am

    “Synonym” does not mean “text replacement”. ptype is not literally expanded to int * by the preprocessor or anything.

    What it means is that you can do things like:

    typedef int *ptype;
    ptype a;
    int *b;
    
    a = b;   // OK
    

    The assignment is valid because ptype and int * are the same type; there is no type conversion or cast required.

    typedef simply lets you give a new name to an existing type. But that name combines every aspect of the existing type into an indivisible entity, such that e.g. ptype a, b; is equivalent to ptype a; ptype b; (and const ptype means “const pointer-to-int” because ptype means “pointer-to-int”).

    In other words, the new names created by typedef behave like built-in keywords as far as declaring things goes, but the actual types represented by those names are the same.

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

Sidebar

Related Questions

I recently found some code like this: typedef int TenInts[10]; void foo(TenInts &arr); What
I recently found this piece of example code from the GNU make documentation that
I recently found the mobipick calendar and I really like it due to its
I recently found this knockoutjs plugin for KineticJS The wiki page includes an example,
I recently found out about auto-properties and like them quite a lot. At this
I recently found this little piece of code for my .vimrc if has(autocmd) Highlight
I recently found out about Cytoscape.js and would like to try out the force-directed
I recently found a code to send emails using python. It was only for
I recently found myself writing a piece of code that executed a Core Data
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2

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.