Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
*Vs++;C中的优先级_C_Pointers - Fatal编程技术网

*Vs++;C中的优先级

*Vs++;C中的优先级,c,pointers,C,Pointers,我无法理解以下C代码的输出: #include<stdio.h> main() { char * something = "something"; printf("%c", *something++); // s printf("%c", *something); // o printf("%c", *++something); // m printf("%c", *something++); // m } #包括 main() { cha

我无法理解以下C代码的输出:

#include<stdio.h>
main()
{
   char * something = "something";
   printf("%c", *something++);  // s
   printf("%c", *something);    // o
   printf("%c", *++something);  // m
   printf("%c", *something++);  // m
}
#包括
main()
{
char*something=“something”;
printf(“%c”,*something++);//s
printf(“%c”,*something);//o
printf(“%c”,*+++某物);//m
printf(“%c”,*something++);//m
}
请帮助:)

有关详细信息,请参阅

printf("%c", *something++);
获取*某物处的字符,然后将其递增('s')

只需获取char(现在是第二个,因为最后一个语句('o')中的增量)

递增,然后获取新位置('m')的字符

获取*某物处的字符,然后将其递增('m')

有关详细信息,请参阅

printf("%c", *something++);
获取*某物处的字符,然后将其递增('s')

只需获取char(现在是第二个,因为最后一个语句('o')中的增量)

递增,然后获取新位置('m')的字符

获取*某物处的字符,然后将其递增('m')

这非常简单

char * something = "something";
指针的赋值

printf("%c\n", *something++);//equivalent to *(something++)
指针是递增的,但递增之前的值是取消引用的,而它是递增之后的值

printf("%c\n", *something);//equivalent to *(something)
printf("%c\n", *something++);//equivalent to *(something++)
指针现在指向上一条语句中递增后的“o”

printf("%c\n", *++something);//equivalent to *(++something)
指针将递增到指向“m”的位置,并在递增指针后取消引用,因为这是预递增的

printf("%c\n", *something);//equivalent to *(something)
printf("%c\n", *something++);//equivalent to *(something++)
与第一个答案相同。 还要注意printf中每个字符串末尾的
'\n'
。它会使输出缓冲区刷新并使行打印。请始终在printf末尾使用
\n

您可能还想看看

这很简单

char * something = "something";
// main entrypoint
int main(int argc, char *argv[])
{
    char * something = "something";

    // increment the value of something one type-width (char), then
    //  return the previous value it pointed to, to be used as the 
    //  input for printf.
    // result: print 's', something now points to 'o'.
    printf("%c", *something++);

    // print the characer at the address contained in pointer something
    // result: print 'o'
    printf("%c", *something);

    // increment the address value in pointer something by one type-width
    //  the type is char, so increase the address value by one byte. then
    //  print the character at the resulting address in pointer something.
    // result: something now points at 'm', print 'm'
    printf("%c", *++something);

    // increment the value of something one type-width (char), then
    //  return the previous value it pointed to, to be used as the 
    //  input for printf.
    // result: print 's', something now points to 'o'.
    printf("%c", *something++);
}
指针的赋值

printf("%c\n", *something++);//equivalent to *(something++)
指针是递增的,但递增之前的值是取消引用的,而它是递增之后的值

printf("%c\n", *something);//equivalent to *(something)
printf("%c\n", *something++);//equivalent to *(something++)
指针现在指向上一条语句中递增后的“o”

printf("%c\n", *++something);//equivalent to *(++something)
指针将递增到指向“m”的位置,并在递增指针后取消引用,因为这是预递增的

printf("%c\n", *something);//equivalent to *(something)
printf("%c\n", *something++);//equivalent to *(something++)
与第一个答案相同。 还要注意printf中每个字符串末尾的
'\n'
。它会使输出缓冲区刷新并使行打印。请始终在printf末尾使用
\n

您可能还想看看

// main entrypoint
int main(int argc, char *argv[])
{
    char * something = "something";

    // increment the value of something one type-width (char), then
    //  return the previous value it pointed to, to be used as the 
    //  input for printf.
    // result: print 's', something now points to 'o'.
    printf("%c", *something++);

    // print the characer at the address contained in pointer something
    // result: print 'o'
    printf("%c", *something);

    // increment the address value in pointer something by one type-width
    //  the type is char, so increase the address value by one byte. then
    //  print the character at the resulting address in pointer something.
    // result: something now points at 'm', print 'm'
    printf("%c", *++something);

    // increment the value of something one type-width (char), then
    //  return the previous value it pointed to, to be used as the 
    //  input for printf.
    // result: print 's', something now points to 'o'.
    printf("%c", *something++);
}
结果:

somm
结果:

somm

始终使用顺时针规则

根据您将首先遇到的规则*因此获取值,然后++表示增量

在第三种情况下,
printf(“%c\n”,*something++);


因此,根据图像增加值+,然后获得值*

始终使用顺时针规则

根据您将首先遇到的规则*因此获取值,然后++表示增量

在第三种情况下,
printf(“%c\n”,*something++);


因此,根据图像增量值++然后得到值*

为什么不包括输出?当编写这样的代码时,添加一些()以增加可读性是很好的,因为它更清楚地知道您喜欢在*(某事++)或(*某物)上发生什么“+,@ Johan:是的,我总是要查找如果++或*是更高的……并且在超过10年的C++之后,,-我总是做*(++的东西)如果任何一个答案帮助你,你应该接受答案;-为什么你不包括输出?当编写这样的代码时,添加一些(好)是好的。增加可读性,因为它更清楚你喜欢*(某物++)或(*某物)++。@Johan:是的,我总是要查找++或*是否更高。。。以及超过10年的C++;我总是这样做*(++某事)如果任何答案对你有帮助,你应该接受答案;-)@MarioTheSpoon奇妙的名称,先生。@MarioTheSpoon奇妙的名称,先生。这是一个很好的参考,但它用于声明中的类型,而不是表达式中的运算符。我在本例中尝试了它,根据我上面给出的解决方案,它工作正常。但是你对它的声明是正确的。如果答案有误导性,我深表歉意。这是一个很好的参考,但它适用于声明中的类型,而不是表达式中的运算符。我在本例中尝试了它,根据我上面给出的解决方案,它工作正常。但是你对它的声明是正确的。如果答案有误导性,我会道歉