如何比较C中的2个字符

时间:2015-06-28 00:39:13

标签: c char

为什么这不起作用? months [5] ==名字相等。 months[5]JunnameJunif永不执行...

 int getMonthNum(char * name){
    char *months[12] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    char *pointertoarray = &months;
    int i;
        for(i = 1; i <= 12; i++){
            if(months[5] == name){
                return i;
            }
        }
    return i;
    }

1 个答案:

答案 0 :(得分:4)

使用strcmp,否则它将比较指针。

.box{
  position: relative;
  float: left;
  width: 20%;
  padding-bottom: 20%;
  margin: 10px 0.5% 0px 0px;
  background: #dd8;
  border-radius: 7px;
 }

 .innerbox{
position: absolute;
top: 10px;
bottom: 10px;
right: 10px;
left: 10px;
overflow: hidden;
}


.innerbox img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
    <div class="box">
        <div class="innerbox">
            <img src="image1.jpg" alt="">
            <div class="title">Title here</div>
        </div>
    </div>
    <div class="box">
        <div class="innerbox">
            <img src="image2.jpg" alt="">
            <div class="title">Title here</div>
        </div>
    </div>
</body>

http://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm