C语言的double pointer的类型转换问题

alittlenewbiek 2020-09-11 03:00:03
关于指针的指针,有两个问题。

一、

// list-1
{
int a = 10;
const int *pa;
pa = &a; // OK
printf("%d\n", *pa);
}

已知,const pointer是可以指向non-const pointer的。
同理,我会认为const pointer pointer也可以指向non-const pointer pointer。
然而,实际必须类型转换后才可以,如list-2

// list-2
{
int a = 10;
int *pa = &a;
const int **ppa = (const int **)&pa; // here
printf("%d\n", **ppa);
}

因此,double pointer必须类型一致是吗?

二、
有这样一段代码

// list-3
{
char *p;
int i = 10;
*(int **)&p = &i;
printf("%d\n", *p);
}

第5行是一种偷鸡取巧的方法,可以让左边的表达式既可以是左值又可以类型转换。
直接(int *)p = &i是不行的,因为类型转换产生的是临时变量,即右值,不可以赋值。
但*(int **)&p不也是类型转换了吗,为什么却可以赋值,偷鸡取巧在哪里?
P.S. 写着写着,似乎有了思路。
类型转换后虽然是右值,但临时空间里存的是毕竟是指针,
而且解第一层指针后再赋值,并非直接对右值赋值,
最终依旧是给p赋值,但类型是int *。
请问,这个思路有没有问题?

P.S. list-3看上去像是个阴间写法,但据我所知,在嵌入式中常用来操作寄存器。
...全文
307 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2020-09-14
  • 打赏
  • 举报
回复
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!
alittlenewbiek 2020-09-12
  • 打赏
  • 举报
回复

依旧会warning。
pointer const pointer中的const是对第二个pointer起作用,
属于top level const,赋值时可以忽略const,故有与没有不影响。
qybao 2020-09-11
  • 打赏
  • 举报
回复
问题一
已知,const pointer是可以指向non-const pointer的。
同理,我会认为const pointer pointer也可以指向non-const pointer pointer。 //这里是错的
应该是 const pointer const pointer 也可以指向 non-const pointer pointer,或 pointer const pointer 也可以指向 non-const pointer pointer


// list-2
{
int a = 10;
int *pa = &a;
const int* const *ppa = &pa; // here,const是针对于最后一层指针
printf("%d\n", **ppa);
}


问题二
你的理解正确
// list-3
{
char *p;
int i = 10;
p = (char*)&i;
*(int*)p = 12; //这里解引用可左值也可右值,所以没问题
printf("%d\n", *p);
}


硬件拾遗 2020-09-11
  • 打赏
  • 举报
回复
说一下 list-3,(int *)p是强制类型转换,这个没问题,*(int **)&p 就不是了,&p 是取p的地址,程序看是个空指针,不考虑,(int **)&p 强制类型转换,而前面 * 是解引用,得到的是左值,不是右值,这样就合理了。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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