此代码有什么问题?它给出错误-左值需要作为增量操作数

时间:2019-10-27 05:51:37

标签: c

我不明白为什么这段代码是错误的,它说需要左值作为增量操作数。但是,行星不是数组指向字符串的指针吗?所以不是Planets ++会将我带到下一个字符串。 example- planets是指向字符串“汞”的指针。当我做行星++时,不应该带我去“金星”

const TabNavigator = createMaterialTopTabNavigator(
  {
    Upcoming: { screen: UpcomingScreen },
    Accepted: { screen: AcceptedScreen },
    Ongoing: { screen: OngoingScreen },
    Completed: { screen: CompletedScreen },
  },
);

UpcomingNav = (props) => {
  props.navigation.navigate('Upcoming')
}

AcceptedNav = (props) => {
  props.navigation.navigate('Accepted')
}

OngoingNav = (props) => {)
  props.navigation.navigate('Ongoing')
}

CompletedNav = (props) => {
  props.navigation.navigate('Completed')
}

const CustomDrawerContentComponent = props => (
    <SafeAreaView style={{flex: 1}}>
      <ScrollView>
        <DrawerItems {...props} />

        <TouchableOpacity onPress={() => this.UpcomingNav(props)}>
            <View style={{flexDirection: 'row', paddingLeft: calcWidth(5), paddingTop: calcHeight(1)}}>
              <Image style={{height: 20, width: 21}} source={require('./images/calendar.png')} />
              <Text style={{fontWeight: 'normal', fontFamily: Fonts.LatoRegular, paddingLeft: calcWidth(10.5), color: 'black'}}>Upcoming Trip</Text>
            </View>
          </TouchableOpacity>

        <TouchableOpacity style={{paddingTop: calcHeight(2)}} onPress={() => this.AcceptedNav(props)}>
            <View style={{flexDirection: 'row', paddingLeft: calcWidth(5), paddingTop: calcHeight(1)}}>
              <Image style={{height: 22, width: 22}} source={require('./images/sticker.png')} />
              <Text style={{fontWeight: 'normal', fontFamily: Fonts.LatoRegular, paddingLeft: calcWidth(10.5), color: 'black'}}>Accepted Trip</Text>
            </View>
          </TouchableOpacity>

          <TouchableOpacity style={{paddingTop: calcHeight(2)}} onPress={() => this.OngoingNav(props)}>
            <View style={{flexDirection: 'row', paddingLeft: calcWidth(5), paddingTop: calcHeight(1)}}>
              <Image style={{height: 22, width: 22}} source={require('./images/navigator.png')} />
              <Text style={{fontWeight: 'normal', fontFamily: Fonts.LatoRegular, paddingLeft: calcWidth(10.5), color: 'black'}}>Ongoing Trip</Text>
            </View>
          </TouchableOpacity>

          <TouchableOpacity style={{paddingTop: calcHeight(2)}} onPress={() => this.CompletedNav(props)}>
            <View style={{flexDirection: 'row', paddingLeft: calcWidth(5), paddingTop: calcHeight(1)}}>
              <Image style={{height: 24, width: 20}} source={require('./images/checklist.png')} />
              <Text style={{fontWeight: 'normal', fontFamily: Fonts.LatoRegular, paddingLeft: calcWidth(10.5), color: 'black'}}>Completed Trip</Text>
            </View>
          </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
);

const DrawerNavigatorExample = createDrawerNavigator({
  Screen1: {
    screen: Screen1_StackNavigator,
    navigationOptions: {
      drawerLabel: () => null,
    },
  },
  Screen2: {
    screen: Screen2_StackNavigator,
    navigationOptions: {
      drawerLabel: () => null,
    },
  },
},{
    contentComponent: CustomDrawerContentComponent,
    contentOptions: {
      labelStyle: {
        fontFamily: Fonts.LatoRegular,
        fontWeight: 'normal'
      }
    },
  },
);

4 个答案:

答案 0 :(得分:3)

您对planets的描述是准确的;它被定义为一个指针数组。强调其中的 array 部分,因为这意味着使用其id不能进行左值操作(如后递增)。

如果要使用指针表示法遍历该指针数组,可以使用键入该数组类型的正确的指针到指针来完成。如果该数组是const char*的数组,则指向const char *的指针(即const char **)是合适的。

赞:

#include <stdio.h>

int main()
{
    const char *planets[] = {
        "Mercury", "Venus", "Earth", "Mars", "Jupiter",
        "Saturn", "Uranus", "Neptune", "Pluto"
    };


    int count = 0;
    for(const char **p = planets; p != planets + 9; ++p)
    {
        if(**p == 'M'){
            count++;
        }// if
    }// for

    printf("%d\n",count);
}

输出

2

答案 1 :(得分:1)

变量planets是一个数组,因此您无法对其进行递增。

请在循环中使用整数作为索引:

#include <stdio.h>

int main()
{

    char* planets[] = {"Mercury", "Venus", "Earth","Mars","Jupiter",
                        " Saturn", "Uranus", "Neptune", "Pluto"};


    int count = 0;
    for(int i = 0; i < 9; i++){
        if(planets[i][0] == 'M'){
            count++;
        }// if 

    }// for

    printf("\n%d",count);

}

答案 2 :(得分:1)

如其他答案所述,char *planets[]定义了一个字符串数组。编译器将planets视为固定在数组上的标签,并且(通常)不允许移动该标签。因此,最好的选择是(如此处其他答案所示)在数组中建立索引,或使用辅助指针遍历数组。

我喜欢为数组添加结尾NULL,当使用辅助指针遍历数组时,它提供了一个停止点。例如:

  #include <stdio.h>

  int main()
    {
    char *planets[] = {"Mercury", "Venus", "Earth","Mars","Jupiter",
                        " Saturn", "Uranus", "Neptune", "Pluto", NULL};
    char **planet;

    int count = 0;
    for(planet = planets; *planet; planet++)
      {
      if(**planet == 'M')
            count++;
      }

    printf("%d\n",count);
    }

答案 3 :(得分:0)

关于:

for(planets; planets <= planets + 8;planets++){  

这将导致编译器发出以下消息:

:11:48: error: lvalue required as increment operand

但是,变量planets位于固​​定在内存中的地址处,因此无法更改。建议

for(char * string = planets;字符串<= planets + 8; string ++)

然后在string循环体内使用变量for()