C/C++语言printf函数输出指针指向的数组的值时,指针的值为什么这样变化?
大家讲道理
大家讲道理 2017-06-27 09:18:57
0
4
1317

1.请问prinf函数的参数中含有指针表达式,是按照什么顺序运算的,代码运算结果中显然不是从左到右进行的。

#include <stdio.h>
int main() {
    int a[5] = { 1,2,3,4,5 };
    int *p = a;
    printf("%d\n", *p);
    printf("%d %d %d %d\n", *(++p)++,*p, *p++, *p);

    getchar();
    return 0;
}

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
某草草

函数参数的运算顺序,跟函数的内部逻辑关系不大,应该是入栈前(函数执行前)先计算++p,函数结束后,再计算p++。如果想知道具体的顺序,可以参照汇编代码(具体含义等我明天更新,抱歉)

    .file    "a.cpp"
    .def    ___main;    .scl    2;    .type    32;    .endef
    .section .rdata,"dr"
LC0:
    .ascii "%d\12\0"
LC1:
    .ascii "%d %d %d %d\12\0"
    .text
    .globl    _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
LFB10:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    pushl    %esi
    pushl    %ebx
    andl    $-16, %esp
    subl    $64, %esp
    .cfi_offset 6, -12
    .cfi_offset 3, -16
    call    ___main
    movl    $1, 40(%esp)
    movl    $2, 44(%esp)
    movl    $3, 48(%esp)
    movl    $4, 52(%esp)
    movl    $5, 56(%esp)
    leal    40(%esp), %eax
    movl    %eax, 60(%esp)
    movl    60(%esp), %eax
    movl    (%eax), %eax
    movl    %eax, 4(%esp)
    movl    $LC0, (%esp)
    call    _printf
    movl    60(%esp), %eax
    movl    (%eax), %ebx
    movl    60(%esp), %eax
    leal    4(%eax), %edx
    movl    %edx, 60(%esp)
    movl    (%eax), %ecx
    movl    60(%esp), %eax
    movl    (%eax), %edx
    addl    $4, 60(%esp)
    movl    60(%esp), %eax
    leal    4(%eax), %esi
    movl    %esi, 60(%esp)
    movl    (%eax), %eax
    movl    %ebx, 16(%esp)
    movl    %ecx, 12(%esp)
    movl    %edx, 8(%esp)
    movl    %eax, 4(%esp)
    movl    $LC1, (%esp)
    call    _printf
    call    ___getreent
    movl    4(%eax), %eax
    movl    %eax, (%esp)
    call    _getc
    movl    $0, %eax
    leal    -8(%ebp), %esp
    popl    %ebx
    .cfi_restore 3
    popl    %esi
    .cfi_restore 6
    popl    %ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE10:
    .ident    "GCC: (GNU) 5.4.0"
    .def    _printf;    .scl    2;    .type    32;    .endef
    .def    ___getreent;    .scl    2;    .type    32;    .endef
    .def    _getc;    .scl    2;    .type    32;    .endef

题外话:
结果中出现的2和3还可以说的通。
4很奇怪,非要勉强地解释的话,*(++p)++括号外面的++也对p起作用了,
但是形如p++运算符,应该在语句结束后才自增的,所以这样解释明显不对。
我在Cygwin + gcc (GCC) 5.4.0环境,运行的结果如下,没有出现4,请问题主用了什么环境?

1
3 2 1 1
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!