简体   繁体   English

在字符串数组中搜索字符串

[英]search of string in an array of strings

i wrote some code that is supposed to find the location of a given string in an array of strings.我写了一些代码来查找给定字符串在字符串数组中的位置。 problem is- it doesn't give the location.问题是 - 它没有给出位置。 it gives something else.它给出了别的东西。

i understand that probably the problem has to do with the differences between the pointers that are involved- a previous version that dealt with finding the position of a letter in a word worked well.我知道问题可能与所涉及的指针之间的差异有关 - 以前的版本处理在单词中查找字母的位置效果很好。 after a lot of attempts to figure out where is the bug, i ask your help.经过多次尝试找出错误所在,我请求您的帮助。 kindly, explain me what should be done.好心,解释我应该怎么做。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int what (char * token);

main()
{
    int i=0;
    char  string[]="jsr";
    char *token;
    token=&string[0];
    i=what(token);
    printf(" location of input is %d \n", i);
    return 0;

}   

int what (char * token)
{
    int i=1;
    char *typtbl[]={"mov",
        "cmp",
        "add",
        "sub",
        "not",
        "clr",
        "lea",

    };  

    char * ptr;

    ptr=(char *)typtbl;
    while (!(strcmp(ptr,token)==0))
    {
        ptr=(char *)(typtbl+i);
        i++;
    }   
    return i;
} 

As pointed out, you did not design function what properly.正如指出的那样,你没有设计功能what正确。 What value should it return if your search function go through all the pointers but does not find the desired string?如果您的搜索函数遍历所有指针但没有找到所需的字符串,它应该返回什么值? Typically in that case return -1 would be a choice to indicate nothing found.通常,在这种情况下,返回-1将是表示未找到任何内容的选择。 Also in this case, using a for loop would probably be more suitable, you can just return the index immediately instead of going through all pointers.同样在这种情况下,使用for循环可能更合适,您可以立即返回索引而不是遍历所有指针。

int what(char *token)
{
    char *typtbl[] = {
        "mov",
        "cmp",
        "add",
        "sub",
        "not",
        "clr",
        "lea",

    };

    for( size_t i = 0; i < sizeof(typtbl)/sizeof(char*); ++i )
    {
        char *ptr = typtbl[i];
        if(strcmp(ptr, token) == 0)
        {
            return i;  // found something
        } 
    }

    return -1;  // found nothing
}

A cleaner working version.一个更干净的工作版本。

Main issue is in the (char *)(typtbl+i) replaced by typtbl[i] in the following code.主要问题在于以下代码中的(char *)(typtbl+i)typtbl[i]替换。 typtbl+i is equivalent to &typtbl[i] , so if my memory is good, it's a pointer on the pointer of the string and not the pointer of string itself typtbl+i等价于&typtbl[i] ,所以如果我记性好,它是字符串指针上的指针,而不是字符串本身的指针

I added a NULL at the end of the array to be able to stop if the string is not present and return -1 to clearly say it was not found.我在数组的末尾添加了一个 NULL,以便在字符串不存在时停止并返回 -1 以明确表示未找到它。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int what(char *token);

int main()
{
    int i = 0;
    char string[] = "jsr";
    i = what(string);
    printf(" location of input is %d \n", i);
    return 0;
}

int what(char *token)
{
    char *typtbl[] = {
        "mov",
        "cmp",
        "add",
        "jsr",
        "not",
        "clr",
        "lea",
        NULL
    };

    int i = 0;
    while(typtbl[i] && !(strcmp(typtbl[i], token) == 0)) {
        ++i;
    }

    if(!typtbl[i])
        i = -1;

    return i;
}

char *token; token=&string[0]; was useless because string == &string[0] .没用,因为string == &string[0]

A few things:一些东西:

  • Your main function is missing its return type.您的主函数缺少返回类型。
  • The while loop in what doesn't stop when the element isn't found.当没有找到该元素,在什么while循环不会停止。 Therefore you are reading out of bounds.因此,您正在阅读越界。

This should do the work w/o pointer arithmetic.这应该可以在没有指针算术的情况下完成工作。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int what (char * token);

int main(){
    int i=0;
    char  string[]="jsr";
    char *token;
    token=&string[0];
    i=what(token);
    printf(" location of input is %d \n", i);
    return 0;
}   

int what (char * token){
    unsigned int i=0;
    char *typtbl[]={"mov",
        "cmp",
        "add",
        "sub",
        "not",
        "clr",
        "lea",

    };  
    unsigned int typtbl_x_size = sizeof(typtbl)/sizeof(typtbl[0]);
    char * ptr;

    ptr=typtbl[i];
    while (!(strcmp(ptr,token)==0)){
        i += 1;
        if (i >= typtbl_x_size){
            printf("element not in list\n");
            return -1;
        }
        ptr=typtbl[i];
    }   
    return i;
}

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

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