简体   繁体   English

c中的运算符和优先级

[英]Operators and precedence in c

I came across the following program. 我遇到了以下程序。

 #include<stdio.h> 
    int main() 
    { 
      char *s[] = { "knowledge","is","power"}; 
      char **p; 
      p = s; 
      printf("%s ", ++*p); 
      printf("%s ", *p++); 
      printf("%s ", ++*p); 

      return 0; 
    }

Output: nowledge nowledge s I'm sure 输出: nowledge nowledge s我确定
1.First printf prints nowledge 1.第一个printf打印nowledge
2.Second printf also prints nowledge because it contains a post increment so original value of first printf is retained. 2.第二个printf也打印nowledge因为它包含后增量,因此保留了第一个printf的原始值。
Now at this stage I'm confused what exactly *p++ does. 现在,在这个阶段,我很困惑* p ++的功能。 Now will p to point to is or owledge . 现在将p指向isowledge

This output of this program is quite confusing because ++*p increments the value pointed to by p which makes different elements of the string array s to get incremented. 该程序的该输出是相当混乱,因为++*p递增值由指向p这使得字符串数组不同元件s得到递增。

  printf("%s ", ++*p);

The value returned by *p gets incremented. *p返回的值将递增。 p points to s[0] . p指向s[0] Therefore will s[0] point to "nowledge" . 因此s[0]指向"nowledge"

  printf("%s ", *p++); 

p still points to the first element of the char * array. p仍指向char *数组的第一个元素。 Because we incremented the value of s[0] by one previously will s[0] still point to "nowledge" . 因为我们递增的值s[0]被一个预先将s[0]仍然指向"nowledge" p gets incremented after the dereference. 取消引用后, p递增。

  printf("%s ", ++*p); 

The value returned by *p gets incremented. *p返回的值将递增。 s[1] will get incremented and point to "s" . s[1]将递增并指向"s"

The value at *p is of type *char (the value of a pointer being an address that is allocated the size of the type it is pointing to) * p处的值是* char类型(指针的值是分配给它所指向的类型的大小的地址)

incrementing it would result in that value being added a sizeof(char) 增加它会导致该值被添加一个sizeof(char)

It's like writing this : 就像这样写:

  printf("%s ", ++(*p)); 

The value at p is of type **char p处的值是** char类型

incrementing it would result in that value added a typeof(*char) . 增加它会导致该值添加了typeof(* char)。

the difference is that the value in the next address holding a *char is the address of the first char in of the next char array 区别在于,下一个持有* char的地址中的值是下一个char数组中第一个char的地址。

I didn't look at the precedence sheet (which i'm sure you can find in simple google search) but from running the code you can understand that ++ precedes * 我没有查看优先级表(我相信您可以在简单的Google搜索中找到它),但是通过运行代码,您可以了解++优先于*

if you want to check it out run this : 如果您想检查出来运行此:

 printf("%s ", (*p)++);
  char *s[] = { "knowledge","is","power"}; // s is an array of pointers
  char **p; // p is a pointer to a pointer to char
  p = s; 
  printf("%s ", ++*p); //(*p) pointing to first string of s, pre-increment so "nowledge" printed
  printf("%s ", *p++); // (*p) printed then post-increment  of p (not *p) hence jumped to next pointer in the array s
  printf("%s ", ++*p); 

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

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