about typedef in the C language

Source: Internet
Author: User

To define a struct in the C language, it is best to use TypeDef, and the typedef is actually a new name for our struct that defines a new type, and when you write your own code, you can directly use the new type first variable that you define.

For example

#include <stdio.h>typedef struct{    int num;    struct Node *next;} Node;int Main () {    Node n;    n.num=111;    printf ("%d", n.num);    return 0;}

By using TypeDef, we define the struct as the new struct type--node, and when used later, you can define the variable directly using Node.

  

#include <stdio.h>struct node{    int num;    struct Node *next;}; int main () {    struct Node n;    n.num=111;    printf ("%d", n.num);    return 0;}

However, when we declare a variable later in the absence of a TypeDef, we must indicate that node is a struct, otherwise the compiler does not know the existence of a node type variable.

In short, in C language, typedef is a struct XX from an alias, convenient to write later.

  

But in C + +, there is no such requirement.

  

#include <iostream>using namespace std;struct node{    int num;    Node *next;}; int main () {    Node n;    n.num=111;    cout<<n.num;    return 0;}

Visible, in C + +, once the struct is declared as a new type, it can be used directly later.

about typedef in the C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.