typedef使用方法实例简介
时间:2010-11-24 来源:盟主仁兄
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
//用途一:为普通变量定义易于记忆的类型名
typedef int Integer;
//用途二:掩饰复合数据类型,如数组和指针
typedef char Line[20];
typedef char* ptr;
//用途三:给链表定义别名
typedef struct Student
{
char StuNo[20];
char StuName[30];
}Stu;
int main()
{
//用途一举例
Integer num = 9;
cout<<"num = "<<num<<endl;
//用途二举例(数组) Line str1,str2; strcpy(str1, "hello"); strcpy(str2, "world"); cout<<"str1 + str2 = "<<str1<<" "<<str2<<endl; //用途二举例(指针) ptr p = str1; for(int i = 0; p[i] != '\0'; p++) { cout<<p[i]<<endl; } //用途三举例 Stu student = {"1001001", "盟主仁兄"}; cout<<"学号:"<<student.StuNo<<" "<<"姓名:"<<student.StuName<<endl; return 0; }
运行结果如下:
//用途二举例(数组) Line str1,str2; strcpy(str1, "hello"); strcpy(str2, "world"); cout<<"str1 + str2 = "<<str1<<" "<<str2<<endl; //用途二举例(指针) ptr p = str1; for(int i = 0; p[i] != '\0'; p++) { cout<<p[i]<<endl; } //用途三举例 Stu student = {"1001001", "盟主仁兄"}; cout<<"学号:"<<student.StuNo<<" "<<"姓名:"<<student.StuName<<endl; return 0; }
运行结果如下:
相关阅读 更多 +