简体   繁体   English

您能否通过执行以下 C 代码来解释为什么会输出:'dcc d'?(指针)

[英]Can you explain why will be output by executing the following C code such that :'d c c d'?(pointers)

My output according to the following code is dcba , but it is wrong.Why?根据以下代码,我的输出是dcba ,但它是错误的。为什么?

char xc[4] = {'a', 'b', 'c', 'd'};
char *xp[4];

int i;
for (i = 0; i < 4; i++) {     
    xp[i] = &xc[i];
}

for (i = 0; i < 4; i++) {     
    *xp[i] = *xp[3-i];
}

printf("%c %c %c %c", xc[0], xc[1], xc[2], xc[3]);

Follow what you did step-by-step.按照你所做的一步一步来。

Your code is equivalent to你的代码相当于

char xc[4] = {'a', 'b', 'c', 'd'};

int i;

for (i = 0; i < 4; i++) {
    xc[i] = xc[3-i];
}

printf("%c %c %c %c", xc[0], xc[1], xc[2], xc[3]);

Then, trace is那么,踪迹是

i statement   xc[0] xc[1] xc[2] xc[3]
(before loop) 'a'   'b'   'c'   'd'
0 xc[0]=xc[3] 'd'   'b'   'c'   'd'
1 xc[1]=xc[2] 'd'   'c'   'c'   'd'
2 xc[2]=xc[1] 'd'   'c'   'c'   'd'
3 xc[3]=xc[0] 'd'   'c'   'c'   'd'

You should use a temporaly variable and be careful to swap the same pair twice to reverse the array.您应该使用临时变量并小心地将同一对交换两次以反转数组。

char xc[4] = {'a', 'b', 'c', 'd'};
char *xp[4];

int i;
for (i = 0; i < 4; i++) {
    xp[i] = &xc[i];
}

for (i = 0; i < 3-i; i++) {
    char t = *xp[i];
    *xp[i] = *xp[3-i];
    *xp[3-i] = t;
}

printf("%c %c %c %c", xc[0], xc[1], xc[2], xc[3]);

Not really complex... If you expand you second for it does:不是真的...复杂如果你展开你第二次for它的作用:

*xp[0] = *xp[3]; // xc[0] receive 'd' value, so xc array is now {'d', 'b', 'c', 'd'}
*xp[1] = *xp[2]; // xc[1] receive 'c' value, so xc array is now {'d', 'c', 'c', 'd'}
*xp[2] = *xp[1]; // xc[2] receive 'c' value, no change
*xp[3] = *xp[0]; // xc[3] receive 'd' value, no change

For i = 0 , xc[0] is modified and have value d .对于i = 0xc[0]被修改并具有值d For i = 1 , xc[1] is modified and have value c .对于i = 1xc[1]被修改并具有值c By this time a and b is no longer anywhere in your xc array.此时ab不再在您的xc数组中。
Now you are using the value of xc[0] and xc[1] to modify the variables xc[3] and xc[2] respectively.现在您正在使用xc[0]xc[1]分别修改变量xc[3]xc[2] The output would be输出将是

d c c d 

In the second loop, you want to use the initial values of xp in order to invert them, but by the time you reach the middle of the array, you have already modified the following values.在第二个循环中,您想使用xp的初始值来反转它们,但是当您到达数组的中间时,您已经修改了以下值。

Declare a temporary variable and also change your printings in order to print xp instead of xc :声明一个临时变量并更改您的打印以打印xp而不是xc

char xc[4] = {'a', 'b', 'c', 'd'};
char *xp[4];
char temp;
int i;

for (i = 0; i < 4; i++) {     
    xp[i] = &xc[i];
}

for (i = 0; i < 4; i++) {  
    temp = *xp[i];   
    *xp[i] = *xp[3-i];
    *xp[3-i] = temp;
}

printf(“%c %c %c %c”, xp[0], xp[1], xp[2], xp[3]);

After the first two iterations of the loop在循环的前两次迭代之后

for (i = 0; i < 4; i++) {     
    *xp[i] = *xp[3-i];
}

the original values of xc[0] and xc[1] were overwritten by values of xc[3] and xc[2] correspondingly and the array became to look like xc[0]xc[1]的原始值被xc[3]xc[2]相应地覆盖,数组变得看起来像

char xc[4] = {'d', 'c', 'c', 'd'};

If you want to reverse the array then the loop can look like如果你想反转数组,那么循环看起来像

size_t i;
size_t n = sizeof( xp ) / sizeof( *xp );

for ( i = 0; i < n / 2; i++ ) 
{
    char c = *xp[i];      
    *xp[i] = *xp[n-i-1];
    *xp[n-i-1] = c;
}

As result you will get that the array will look like结果你会得到数组看起来像

char xc[4] = {'d', 'c', 'b', 'a'};

That is you need to swap characters of the first half of the array with characters of the second half of the array.也就是说,您需要将数组前半部分的字符与数组后半部分的字符交换。

Using this approach you can write a general function.使用这种方法,您可以编写一个通用函数。 For example例如

#include <stdio.h>

void reverse( char s[], size_t n )
{
    size_t i;

    for ( i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n-i-1];
        s[n-i-1] = c;
    }
}    

int main( void ) 
{
    char xc[] = { 'a', 'b', 'c', 'd' };

    printf( "%c %c %c %c\n", xc[0], xc[1], xc[2], xc[3] );

    reverse( xc, sizeof( xc ) / sizeof( *xc ) );

    printf( "%c %c %c %c\n", xc[0], xc[1], xc[2], xc[3] );
}    

The program output is程序输出是

a b c d
d c b a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM