在centos上安装tcl
typedef
is a keyword used in C language to assign alternative names to existing datatypes. Its mostly used with user defined datatypes, when names of the datatypes become slightly complicated to use in programs. Following is the general syntax for using typedef
,
typedef
是使用C语言的关键字,用于为现有数据类型分配备用名称。 当数据类型的名称在程序中使用变得稍微复杂时,它通常与用户定义的数据类型一起使用。 以下是使用typedef
的一般语法:
typedef <existing_name> <alias_name>
Lets take an example and see how typedef
actually works.
让我们举个例子,看看typedef
是如何工作的。
typedef unsigned long ulong;
The above statement define a term ulong
for an unsigned long
datatype. Now this ulong
identifier can be used to define unsigned long
type variables.
上面的语句为unsigned long
数据类型定义了ulong
一词。 现在,此ulong
标识符可用于定义unsigned long
型变量。
ulong i, j;
typedef的应用 (Application of typedef)
typedef
can be used to give a name to user defined data type as well. Lets see its use with structures.
typedef
也可以用于为用户定义的数据类型命名。 让我们看看它在结构中的用法。
typedef struct
{
type member1;
type member2;
type member3;
} type_name;
Here type_name represents the stucture definition associated with it. Now this type_name can be used to declare a variable of this stucture type.
在此, type_name表示与之关联的结构定义。 现在,可以使用此type_name声明此结构类型的变量。
type_name t1, t2;
使用typedef进行结构定义 (Structure definition using typedef)
Let's take a simple code example to understand how we can define a structure in C using typedef
keyword.
让我们看一个简单的代码示例,以了解如何使用typedef
关键字在C中定义结构。
#include<stdio.h>
#include<string.h>
typedef struct employee
{
char name[50];
int salary;
}emp;
void main( )
{
emp e1;
printf("\nEnter Employee record:\n");
printf("\nEmployee name:\t");
scanf("%s", e1.name);
printf("\nEnter Employee salary: \t");
scanf("%d", &e1.salary);
printf("\nstudent name is %s", e1.name);
printf("\nroll is %d", e1.salary);
}
typedef
和指针 (typedef
and Pointers)
typedef
can be used to give an alias name to pointers also. Here we have a case in which use of typedef
is beneficial during pointer declaration.
typedef
也可以为指针赋予别名。 在这种情况下,在指针声明期间使用typedef
很有用。
In Pointers *
binds to the right and not on the left.
在指针中, *
绑定到右侧而不是左侧。
int* x, y;
By this declaration statement, we are actually declaring x
as a pointer of type int
, whereas y
will be declared as a plain int
variable.
通过此声明语句,我们实际上是将x
声明为int
类型的指针,而y
将声明为纯int
变量。
typedef int* IntPtr;
IntPtr x, y, z;
But if we use typedef
like we have used in the example above, we can declare any number of pointers in a single statement.
但是,如果像上面的示例中那样使用typedef
,则可以在单个语句中声明任意数量的指针。
NOTE: If you do not have any prior knowledge of pointers, do study Pointers first.
注意:如果您没有任何有关指针的先验知识,请首先研究指针。
在centos上安装tcl