谁能帮我解释一下这句话是什么意思呢

liangkdm 2013-09-26 10:54:36

#include <stdio.h>

int main()
{
int i = 2;
int *p = &i;

p++;

p[--i] += i;

return 0;

}


p[--i] += i;这句话的意思不是很明白

谢谢大家乐
...全文
163 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
享受编程乐趣 2013-09-26
  • 打赏
  • 举报
回复
看到这里的代码,我也想试一下,代码如下
#include <stdio.h>

int main()
{
int i = 2;
int *p = &i;
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
p++;
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
p[--i] = i; /*p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧
p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p
所指向的地址的内容,这肯定是非法的*/
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
printf("*(P+1):%d\n",*(p+1));
printf("(P+!):%d\n\n",(p+1));
p--;
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
*p=i;
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
p++;
//*p=i; //这一句存在内存错误
printf("*P:%d\n",*p);
printf("P:%d\n\n",p);
return 0;
}

不执行最后面的*p=i;时,结果如下:

当执行最后面的*p=i;时,结果如下:

执行最后面的*p=i;时,返回结果如下,是常见的内存错误

我想知道,最初,p指向的还是i的地址,之后p相对于i所指向的地址进行了移动(p++;和p--;)当最后的*p=i;时,p相当于(&i+1),那么为什么这样程序会崩溃呢,我猜是(&i+1)所指向的地址不允许访问,非法访问所以崩溃,指针实在博大精深,我没掌握好,请大家帮忙解惑?
赵4老师 2013-09-26
  • 打赏
  • 举报
回复
//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 |
//+------------------+-----------------------------------------+---------------+
  • 打赏
  • 举报
回复
引用 14 楼 qq523176585 的回复:
[quote=引用 13 楼 u010059138 的回复:] [quote=引用 12 楼 qq523176585 的回复:] p[--i] += i;你这是非法改变内存的值 p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p所指向的地址的内容,这肯定是非法的
多谢多谢 终于搞明白p[i]是什么意思了 它是值不是地址哈 这样写p[i]真够蛋疼的 那我顺便问一下 既然p[i]是合法的 那么它到底有啥优点呢?感觉没有*(p+i)直接 难道仅仅是书写方便?[/quote] 个人喜好,*(p+i)更直接。比如一个数组a[5],你要访问第二个元素,可以这样写: a[1],*(a+1),1[a]....[/quote] 当然也能这样,比如: char *str = "abcdefg"; char *p = str+2; 那么写成p[-1],也是合法的,[]你可以理解为一个标量,一个偏移,合法即可。
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 14 楼 qq523176585 的回复:
[quote=引用 13 楼 u010059138 的回复:] [quote=引用 12 楼 qq523176585 的回复:] p[--i] += i;你这是非法改变内存的值 p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p所指向的地址的内容,这肯定是非法的
多谢多谢 终于搞明白p[i]是什么意思了 它是值不是地址哈 这样写p[i]真够蛋疼的 那我顺便问一下 既然p[i]是合法的 那么它到底有啥优点呢?感觉没有*(p+i)直接 难道仅仅是书写方便?[/quote] 个人喜好,*(p+i)更直接。比如一个数组a[5],你要访问第二个元素,可以这样写: a[1],*(a+1),1[a]....[/quote] 非常非常感谢 第一次碰到还可以1[a]这样 长见识了
  • 打赏
  • 举报
回复
引用 13 楼 u010059138 的回复:
[quote=引用 12 楼 qq523176585 的回复:] p[--i] += i;你这是非法改变内存的值 p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p所指向的地址的内容,这肯定是非法的
多谢多谢 终于搞明白p[i]是什么意思了 它是值不是地址哈 这样写p[i]真够蛋疼的 那我顺便问一下 既然p[i]是合法的 那么它到底有啥优点呢?感觉没有*(p+i)直接 难道仅仅是书写方便?[/quote] 个人喜好,*(p+i)更直接。比如一个数组a[5],你要访问第二个元素,可以这样写: a[1],*(a+1),1[a]....
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 12 楼 qq523176585 的回复:
p[--i] += i;你这是非法改变内存的值 p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p所指向的地址的内容,这肯定是非法的
多谢多谢 终于搞明白p[i]是什么意思了 它是值不是地址哈 这样写p[i]真够蛋疼的 那我顺便问一下 既然p[i]是合法的 那么它到底有啥优点呢?感觉没有*(p+i)直接 难道仅仅是书写方便?
  • 打赏
  • 举报
回复
p[--i] += i;你这是非法改变内存的值 p[--i]相当于 *(p+ --i),也就是*(p+1),这样的话你吧p指向了i的地址后的2*4 = 8个字节。然后把i值赋给p所指向的地址的内容,这肯定是非法的
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 8 楼 lin5161678 的回复:
[quote=引用 6 楼 u010059138 的回复:] 虽然是未定义 但是编译器还是可以运行 我主要是想弄清楚p[i]这个到底是什么意思 是值还是地址
p[i]是作死 指针越界 [/quote] 现在我想了解下最后p[i]为什么等于1呢? p[i]是值?不是地址?
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 9 楼 zhxianbin 的回复:
这又不知道是哪家蛋疼的笔试题吧
嗯哪 富士通的
zhxianbin 2013-09-26
  • 打赏
  • 举报
回复
这又不知道是哪家蛋疼的笔试题吧
lin5161678 2013-09-26
  • 打赏
  • 举报
回复
引用 6 楼 u010059138 的回复:
虽然是未定义 但是编译器还是可以运行 我主要是想弄清楚p[i]这个到底是什么意思 是值还是地址
p[i]是作死 指针越界
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 5 楼 Idle_Cloud 的回复:
p[i]基于p的地址偏移i*4 就是p的地址 + 4*i 当然这个操作在你的这段代码里是很危险的。
那您的意思是 p[i]这个是地址? 怎么也有人说是值呢? 能详细点吗? 谢谢您啦
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 1 楼 lin5161678 的回复:
p[--i] += i;意思是 错误的代码 未定义行为
虽然是未定义 但是编译器还是可以运行 我主要是想弄清楚p[i]这个到底是什么意思 是值还是地址
Carl_CCC 2013-09-26
  • 打赏
  • 举报
回复
p[i]基于p的地址偏移i*4
就是p的地址 + 4*i
当然这个操作在你的这段代码里是很危险的。
lin5161678 2013-09-26
  • 打赏
  • 举报
回复
引用 2 楼 Idle_Cloud 的回复:
p[--i] += i; 相当于两句 --i; p[i] = p[i] + i;
没这回事 说错了 误导人
liangkdm 2013-09-26
  • 打赏
  • 举报
回复
引用 2 楼 Idle_Cloud 的回复:
p[--i] += i; 相当于两句 --i; p[i] = p[i] + i;
这个我明白 我就是对p[i]这个是 什么意思不是很明白
Carl_CCC 2013-09-26
  • 打赏
  • 举报
回复
p[--i] += i;
相当于两句
--i;
p[i] = p[i] + i;
lin5161678 2013-09-26
  • 打赏
  • 举报
回复
p[--i] += i;意思是 错误的代码 未定义行为

69,371

社区成员

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

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