C++新手求助,关于typedof,const。路过求帮助,谢谢。

WinddStarr 2015-11-24 09:31:00
最近在看C++ Primer,有些疑问。今天这个如下:
语句:typedef string *pstring;
const pstring cstr;
与语句:string *const cstr;
等价。
后者我能看懂,定义了一个指向string对象的const指针,前者的话,typedef string *pstring这句是什么意思?string *pstring是定义了一个未初始化的指向string对象的普通指针,前面加个typedef是什么意思?是说*pstring 是string的别名,那const pstring cstr又是何意?pstring是一个指针,现在给他const限定不是应该初始化么?后面跟个cstr是什么用法?定义cstr?初学C++,很多都不懂,去网上查了一些感觉是连锁关系,想看懂一个得再看另一个,而迷失了最初的问题,也丢失了学习的明确路径。请诸位路过的前辈帮忙解疑答惑,谢谢。
...全文
153 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2015-11-27
  • 打赏
  • 举报
回复
引用 7 楼 WinddStarr 的回复:
谢谢。请问是不是c++语法li用typedef定义已有类型的新名字时用到了操作符都是修饰已有类型的?比如说typedef char *pchar,typedef int int_array[4],这里*,[4]分别修饰的是char,int。
可以像你说那么理解 pchar代替char* int_array代替int[4]
赵4老师 2015-11-27
  • 打赏
  • 举报
回复
//char (*(*x[3])())[5];//x是什么类型的变量?
//
//分析C语言声明,关键是搞清楚这个变量是个什么东西(函数、指针、数组),
//是函数那么剩下的就是他的参数和返回值,
//是指针那剩下部分是说明他指向什么,
//是数组剩下的部分就是说明数组的成员是什么类型。
//解析C语言声明规则:
//从左侧第一个标识符开始,按照优先级进行结合。*表示是..的指针,const表示只读的,volatile表示可变的,[]表示是数组,()表示是函数。
//
//x和[3]结合说明是一个大小为3的数组,该数组的每个元素为一类指针,该类指针指向一类函数,该类函数无参数,返回一类指针,该类指针指向一个大小为5的char型数组
#include <stdio.h>
#include <typeinfo.h>
char num[5];
char (*x00())[5] {
    return #
}
int main() {
    char (*(*x[3])())[5];//是个数组,大小为3
    char (*(*x0  )())[5];//数组的元素,是个函数指针
    char (*( x00 )())[5];//函数原型,参数为空,返回值为指针
    char (*  x000   )[5];//返回值

    x0 = x00;
    x[0] = x0;
    x[1] = x0;
    x[2] = x0;
    printf("typeid(x).name() is %s\n",typeid(x).name());
    return 0;
}
//typeid(x).name() is char (* (__cdecl**)(void))[5]
WinddStarr 2015-11-26
  • 打赏
  • 举报
回复
谢谢。请问是不是c++语法li用typedef定义已有类型的新名字时用到了操作符都是修饰已有类型的?比如说typedef char *pchar,typedef int int_array[4],这里*,[4]分别修饰的是char,int。
赵4老师 2015-11-25
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"
paschen 版主 2015-11-25
  • 打赏
  • 举报
回复
用pstring代替string*整体
WinddStarr 2015-11-25
  • 打赏
  • 举报
回复
谢谢,看明白了。那在用typedef string *pstring里默认*是和string 在一起的(指向string对象的指针)而不是和pstring连在一起的。我开始还以为两种都行呢,认为是没说清楚。那后面的根据const在类型前类型后等价就可以了。再次谢谢!
lm_whales 2015-11-24
  • 打赏
  • 举报
回复
因为 typedef 定义的类型,是一个整体,不可分割. const string *s; /// const 修饰的是string 这个类型 const pstring s; /// const 修饰的同样 是pstring 这个类型。 string * const s; /// const 修饰的是 string * 这个类型,不是修饰 * 号。 pstring 这个类型恰恰就是 string * 从这个角度,就可以理解 const pstring 就是 string*const 了; 另外 const type 和 type const 是等效的 这样也可以理解 const pstring 就是 pstring const 也就是 string *const 了 , const string *s;中, 这里 const 修饰string 因为 string 和* 是两个符号, string *并不是一个类型整体。 const string * 这三个符号 才构成一个类型整体 也就是说, 直接定义(声明)的时候,const 修饰符号,指针运算符 * 。类型 string 是三个独立的符号。 这三个符号,合在一起,是这个声明中的 一个完整的类型。 而 typedef 定义后的 pstring 本身就是完整的,不是 string * 两个符号的连写。 不能把它看成分开的两个符号。
WinddStarr 2015-11-24
  • 打赏
  • 举报
回复
谢谢!这里的pstring cstr;相当于string *cstr;只不过可以用来同时定义多个指针(pstring cstr1,cstr2;等价于string *cstr1,*cstr2;),剩下的问题是为什么const pstring cstr;中const修饰的是cstr而不是pstring?是规定还是其他?希望前辈解释下。如果const修饰的是cstr,定义了一个指向string类型的const指针,为何没初始化?
fefe82 2015-11-24
  • 打赏
  • 举报
回复
请在你的教材里搜索 typedef 。 如果你没有教材的话,就去找一本。 ====== typedef 是用来给类型定义别名的 http://en.cppreference.com/w/cpp/language/typedef

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧