C/C++--typedef

本文介绍了typedef的四个用途,包括同时声明指针型对象、简写结构体对象、解决跨平台问题、为函数和数组类型重命名;指出两大陷阱,如与const结合时不是简单替换、不能和存储类关键字连用;还对比了typedef与#define的区别,强调typedef不是简单字符串替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一、四个用途

用途一:

1.用作同时声明指针型的多个对象:

typedef char* PCHAR;    // 一般用大写
PCHAR pa, pb;               // 同时声明了两个指向字符变量的指针

2.替代:char *pa, *pb; 

3.而 char* pa, pb; 含义是声明了一个指向字符变量的指针;和一个字符变量;

用途二:

在C中申明结构体对象:

struct tagPOINT1  
{  
    int x;  
    int y;  
};  
struct tagPOINT1 p1;   

每次第一个结构体对象要多写一个struct,利用typedef 可以达到简写目的。

typedef struct tagPOINT  
{  
    int x;  
    int y;  
}POINT;  
POINT p1; // 这样就比原来的方式少写了一个struct,比较省事,尤其在大量使用的时候 

用途三:

解决跨平台问题,比如标准库中的size_t

用途四:

1.为函数类型重命名(typedef有两类自定义类型)

函数名就是一个指针,当调用一个函数时,我们都是直接用函数名调用,或者说通过指针调用。

明白函数名的特点后,看第一个typedef定义的类型

a.类型一,指针

#include <stdio.h>  
typedef int (*fp_t)(char c);  
  
int f0(char c) { printf("f0, c = %c\n", c); return 0;}  
int f1(char c) { printf("f1, c = %c\n", c); return 1;}  
  
int main()  
{  
        int ret;  
        fp_t fp;  
        fp = f0;  
        ret = fp('a');  
        fp = f1;  
        ret = fp('x');  
        return 0;  
}  

类型二,非指针

#include <stdio.h>  
typedef int fp_t(char c);  
  
int f0(char c) { printf("f0, c = %c\n", c); return 0;}  
int f1(char c) { printf("f1, c = %c\n", c); return 1;}  
  
int main()  
{  
        int ret;  
        fp_t* fp;  
        fp = f0;  
        ret = fp('a');  
        fp = f1;  
        ret = fp('x');  
        return 0;  
}  

2.type定义数组类型

1. 一维数组类型的定义格式
typedef <元素类型关键字><数组类型名>[<常量表达式>];
例如:
(1) typedef int vector[10];
(2) typedef char strings[80];
(3) typedef short int array[N];

(1) vector v1,v2;
(2) strings s1,s2="define type";
(3) array a={25,36,19,48,44,50};  //假定常量N≥6

2. 二维数组类型的定义格式

typedef <元素类型关键字><数组类型名>[<常量表达式1>][<常量表达式2>];

例如:
(1) typedef int matrix[5][5];
(2) typedef char nameTable[10][NN];
(3) typedef double DD[M+1][N+1];

(1) matrix mx={{0}};
(2) nameTable nt={""};  //或使用等同的{{'\0'}}初始化
(3) DD dd={{0.0}};

第二、两大陷阱

陷阱一:

记住,typedef是定义了一种类型的新别名,不同于宏,它不是简单的字符串替换。比如:
先定义:
typedef char* PSTR;
然后:
int mystrcmp(const PSTR, const PSTR);

const PSTR实际上相当于const char*吗?不是的,它实际上相当于char* const。
原因在于const给予了整个指针本身以常量性,也就是形成了常量指针char* const。
简单来说,记住当const和typedef一起出现时,typedef不会是简单的字符串替换就行。

陷阱二:

typedef在语法上是一个存储类的关键字(如auto、extern、mutable、static、register等一样),虽然它并不真正影响对象的存储特性,如:
typedef static int INT2; //不可行
编译将失败,会提示“指定了一个以上的存储类”。

 

第三、typedef 与 #define的区别

案例一:

通常讲,typedef要比#define要好,特别是在有指针的场合。请看例子:

  1. typedef char *pStr1;  
  2. #define pStr2 char *;  
  3. pStr1 s1, s2;  
  4. pStr2 s3, s4;  

在上述的变量定义中,s1、s2、s3都被定义为char *,而s4则定义成了char,不是我们所预期的指针变量,根本原因就在于#define只是简单的字符串替换而typedef则是为一个类型起新名字。

案例二:

下面的代码中编译器会报一个错误,你知道是哪个语句错了吗?

  1. typedef char * pStr;  
  2. char string[4] = "abc";  
  3. const char *p1 = string;  
  4. const pStr p2 = string;  
  5. p1++;  
  6. p2++;  

是p2++出错了。这个问题再一次提醒我们:typedef和#define不同,它不是简单的文本替换。上述代码中const pStr p2并不等于const char * p2。const pStr p2和const long x本质上没有区别,都是对变量进行只读限制,只不过此处变量p2的数据类型是我们自己定义的而不是系统固有类型而已。因此,const pStr p2的含义是:限定数据类型为char *的变量p2为只读,因此p2++错误。

FAILED: cpp/inspireface/CMakeFiles/InspireFace.dir/c_api/inspireface.cc.o D:\android\Android\Sdk\ndk\25.2.9519653\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android21 --sysroot=D:/android/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/windows-x86_64/sysroot -DFEATURE_BLOCK_ENABLE_OPENCV -DINFERENCE_WRAPPER_ENABLE_MNN -DISF_BUILD_SHARED_LIBS -DInspireFace_EXPORTS -DSODIUM_STATIC -ID:/insightface-master/insightface-master/cpp-package/inspireface/3rdparty/InspireCV/3rdparty/Eigen-3.4.0-Headers -ID:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/. -ID:/insightface-master/insightface-master/cpp-package/inspireface/3rdparty/MNN/include -ID:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/libsodium-cmake-master/libsodium/src/libsodium/include -ID:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/libcorrect/include -ID:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/include/inspireface -ID:/insightface-master/insightface-master/cpp-package/inspireface/3rdparty/yaml-cpp/include -ID:/insightface-master/insightface-master/cpp-package/inspireface/3rdparty/inspireface-precompile-lite/sqlite -ID:/insightface-master/insightface-master/cpp-package/inspireface/3rdparty/InspireCV/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -g0 -std=c++14 -O3 -O3 -mfpu=neon -O3 -DNDEBUG -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT cpp/inspireface/CMakeFiles/InspireFace.dir/c_api/inspireface.cc.o -MF cpp\inspireface\CMakeFiles\InspireFace.dir\c_api\inspireface.cc.o.d -o cpp/inspireface/CMakeFiles/InspireFace.dir/c_api/inspireface.cc.o -c D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc clang++: warning: argument unused during compilation: '-mfpu=neon' [-Wunused-command-line-argument] In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:8: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface_internal.h:9: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./engine/face_session.h:13: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/face_track_module.h:9: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/face_detect/face_detect_adapt.h:10: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./middleware/any_net_adapter.h:15: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./middleware/model_archive/inspire_archive.h:15: D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:13:15: warning: anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here [-Wnon-c-typedef-for-linkage] typedef struct { ^ SemanticIndex D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:14:31: note: type is not C-compatible due to this default member initializer int32_t left_eye_center = 67; ^~ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:23:3: note: type is given name 'SemanticIndex' for linkage purposes by this typedef declaration } SemanticIndex; ^ In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:8: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface_internal.h:9: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./engine/face_session.h:13: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/face_track_module.h:16: D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/tracker_optional/bytetrack/BYTETracker.h:33:141: warning: implicit conversion from 'long' to 'float' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-const-int-float-conversion] double lapjv(const vector<vector<float> > &cost, vector<int> &rowsol, vector<int> &colsol, bool extend_cost = false, float cost_limit = LONG_MAX, ~ ^~~~~~~~ D:/android/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/14.0.7/include/limits.h:47:19: note: expanded from macro 'LONG_MAX' #define LONG_MAX __LONG_MAX__ ^~~~~~~~~~~~ <built-in>:84:22: note: expanded from here #define __LONG_MAX__ 9223372036854775807L ^~~~~~~~~~~~~~~~~~~~ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:117:20: error: use of undeclared identifier 'token' result->size = token.size(); ^ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:118:37: error: use of undeclared identifier 'token' result->data = (uint8_t*)malloc(token.size()); ^ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:119:26: error: use of undeclared identifier 'token' memcpy(result->data, token.data(), token.size()); ^ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/c_api/inspireface.cc:119:40: error: use of undeclared identifier 'token' memcpy(result->data, token.data(), token.size()); ^ 2 warnings and 4 errors generated. [545/568] Building CXX object cpp/inspireface/CMakeFiles/InspireFace.dir/track_module/face_track_module.cpp.o clang++: warning: argument unused during compilation: '-mfpu=neon' [-Wunused-command-line-argument] In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/face_track_module.cpp:6: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/face_track_module.h:9: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/face_detect/face_detect_adapt.h:10: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./middleware/any_net_adapter.h:15: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./middleware/model_archive/inspire_archive.h:15: D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:13:15: warning: anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here [-Wnon-c-typedef-for-linkage] typedef struct { ^ SemanticIndex D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:14:31: note: type is not C-compatible due to this default member initializer int32_t left_eye_center = 67; ^~ D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/./track_module/landmark/landmark_param.h:23:3: note: type is given name 'SemanticIndex' for linkage purposes by this typedef declaration } SemanticIndex; ^ In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/face_track_module.cpp:6: In file included from D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/face_track_module.h:16: D:/insightface-master/insightface-master/cpp-package/inspireface/cpp/inspireface/track_module/tracker_optional/bytetrack/BYTETracker.h:33:141: warning: implicit conversion from 'long' to 'float' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-const-int-float-conversion] double lapjv(const vector<vector<float> > &cost, vector<int> &rowsol, vector<int> &colsol, bool extend_cost = false, float cost_limit = LONG_MAX, ~ ^~~~~~~~ D:/android/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/14.0.7/include/limits.h:47:19: note: expanded from macro 'LONG_MAX' #define LONG_MAX __LONG_MAX__ ^~~~~~~~~~~~ <built-in>:84:22: note: expanded from here #define __LONG_MAX__ 9223372036854775807L ^~~~~~~~~~~~~~~~~~~~ 2 warnings generated. [546/568] Building CXX object cpp/inspireface/CMakeFiles/I...track_module/tracker_optional/bytetrack/kalmanFilter.cpp.o clang++: warning: argument unused during compilation: '-mfpu=neon' [-Wunused-command-line-argument] [547/568] Building C object cpp/inspireface/CMakeFiles/Ins...__/3rdparty/inspireface-precompile-lite/sqlite/sqlite3.c.o ninja: build stopped: subcommand failed. Build-AndroidArch : Ninja 编译失败! 所在位置 D:\insightface-master\insightface-master\cpp-package\inspireface\command\build_android.ps1:247 字符: 1 + Build-AndroidArch "arm64-v8a" 21 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Build-AndroidArch
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值