标题:if (found == strstr(catalog, ","))的使用问题——大神求助!
取消只看楼主
XiaoXiao_Ren
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
威 望:1
帖 子:80
专家分:198
注 册:2013-7-17
结帖率:100%
已结贴  问题点数:20 回复次数:1 
if (found == strstr(catalog, ","))的使用问题——大神求助!
请教大神:

        程序代码中
        char *found;
char *found 被定义之后,found是否已被分配了地址空间,
found无论什么时候都不会等于0,这样理解正确吗?


如果这样理解正确那么请问:
if (found == strstr(catalog, ","))的使用进入不到if里面吧。
【catalog是字符数组名,里面存放如“mycd,213,zhoujielun”】
【strstr是在catalog中查找“,”这个字符串在catalog中第一次出现的位置并返回,没找到返回null】
但在if中做判断的时候,为什么每次都可以成立。
搜索更多相关主题的帖子: zhoujielun 字符串 null 空间 
2013-08-18 22:21
XiaoXiao_Ren
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
威 望:1
帖 子:80
专家分:198
注 册:2013-7-17
得分:0 
以下是相应的代码:问题解决了,确实是因为我的判断语句之后多了一个分号,所以每次if判断都进入;另外我觉得这里应该是用赋值=语句才对,可能是作者疏忽吧。。
                  谢谢大家帮忙!

void find_cd()
{
        char match[MAX_STRING], entry[MAX_ENTRY];
        FILE *titles_fp;
        int count = 0;
        char *found, *title, *catalog;

        mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: ");
        get_string(match);

        titles_fp = fopen(title_file, "r");
        if (titles_fp) {
                while (fgets(entry, MAX_ENTRY, titles_fp)) {
                        catalog = entry;
                        if (found == strstr(catalog, ",")); {
                                *found = '\0';
                                title = found + 1;

                                if (found == strstr(title, ",")) {
                                        *found = '\0';
                                        if (found == strstr(title, match)) {
                                                count++;
                                                strcpy(current_cd, title);
                                                strcpy(current_cat, catalog);
                                        }
                                }
                        }
                }
                fclose(titles_fp);
        }
        if (count != 1) {
                if (count == 0) {
                        mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found. ");
                }
                if (count > 1) {
                        mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous: %d CDs found. ",count);
                }
                current_cd[0] = '\0';
                get_return();
        }
}

否极泰来
2013-08-19 13:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419341-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.358864 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved