目前我正在学习C并试图了解这些说明。他们真的不一样吗?
++*argv
*argv++
*(++argv)
*(argv++)
谢谢!
答案 0 :(得分:4)
后缀增量运算符的优先级高于指针去引用运算符,而不是前缀增量。所以这两个是等价的:
*p++ *(p++)
前缀增量具有与*相同的优先级,因此* ++ p递增指针,并且与*(++ p)相同。此外,++ * p与++(* p)相同。
答案 1 :(得分:2)
看看下面的代码。
main()
{
int a[4] = { 10,20,30,40};
int *argv = a;
t = ++*argv;
printf("%d\n",*argv); /* Here *argv is 11 */
printf("%d\n",t); /* Here t is 11 because of pre-increment */
*argv++; /* argv is incremented first ++ has higher priority over "*" */
printf("%d\n",*argv);/* *argv is printed which is 20 */
*(++argv); /* argv is incremented first ++ has higher priority over "*" */
printf("%d\n",*argv); /* *argv is 30 */
*(argv++); /* As explained above the same applies here also */
printf("%d\n",*argv);
}
答案 2 :(得分:0)
当您在赋值中使用预增量和后增量运算符(即++ argv和argv ++)时,您必须了解rvalue和lvlue.Like是否将首先递增变量值,然后将其分配给LHS或在分配之后对于LHS,变量值会增加。括号也会改变优先级。需要理解概念优先级,左值和r值以及指针增量规则。
答案 3 :(得分:0)
增量的两个小例子
提示:为了更好地理解,请尝试将// import dependencies
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import axios from 'axios'
// import components
import '../Styles/Css/PostsCategories.css'
import { createSearchUrl } from './SharedModules'
class PostsCategories extends Component {
constructor() {
super()
this.state = {
categories: null,
loading: false
}
}
componentDidMount() {
this.setState({
loading: true
})
axios.get(`http://localhost/wordpress-api/wp-json/wp/v2/categories`)
.then(res => {
this.setState({
categories: res.data,
loading: false
})
})
}
isCategoryActive = (category) => {
let newCategories = this.props.searchCategories
newCategories.indexOf(category) === -1
? newCategories.push(category)
: newCategories.splice(newCategories.indexOf(category),1)
return newCategories
}
render() {
if (this.state.loading || !this.state.categories) return <div className='posts-categories'><h2 className='loading'>Loading ...</h2></div>
const categories = this.state.categories.map(category => {
const newCategories = this.isCategoryActive(category.id)
const url = createSearchUrl('/blog', newCategories, this.props.searchString)
console.log(newCategories, url)
return (
<Link
to={url}
onClick={this.props.searchCategoryChange.bind(this, category.id)}
className='posts-category'
key={category.id} >
{category.name}
</Link>
)})
return (
<div className='posts-categories'>
{categories}
</div>
)
}
}
export default PostsCategories
想象为1或2。
在下面的程序中,所有参数都打印,包括程序名称 argc
。
argv[0]
在下面的程序中,所有参数都打印,但程序名称除外 int
main(int argc, char **argv)
{
while (argc--)
printf("%s\n", *argv++); /* same as *(argv++) */
}
。
argv[0]
int
main(int argc, char **argv)
{
while (--argc)
printf("%s\n", *(++argv));
}
会增加值++*argv
。