在字符串数组中搜索字符串 [英] search a string in an array of strings

查看:109
本文介绍了在字符串数组中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到坏指针。谁能告诉我什么我做错了?

  INT SearchString在(字符*改编[],字符*键,INT大小)
{
INT N;
为(N = 0; N<大小; ++ N){
    如果(STRCMP(ARR [N],键)){
        返回N;
    }
}
返回-1;}


 字符海峡[] [16] = {MOV,CMP,增加,分,执法机关,不是,CLR,公司,月,JMP,BNE,红,JRN,PSR,RTS,停止};    如果(SearchString在(STR,字,16)== -1){返回FALSE;}


解决方案

不能告诉你在哪里从起源。你可能想如果返回N(STRCMP(ARR [N]键)!); (相反)。和阵列的类型可能不是你想要的。尝试

 为const char *海峡[] = {MOV,...};

来代替。你有字符数组的数组,并通过它,你实际上是希望指针数组。

I keep getting bad pointers. Can anyone tell me what am I doing wrong?

int SearchString( char* arr[], char* key, int size )
{
int n;
for ( n = 0; n < size; ++n ) {
    if ( strcmp(arr[n], key) ) { 
        return n;
    } 
} 
return -1;

}


char str[][16] = { "mov","cmp","add","sub","lea","not","clr","inc","dec","jmp","bne","red","jrn","psr","rts","stop"};

    if(SearchString(str,"word",16) == -1){  return FALSE;}

解决方案

Can't tell where your word originates from. You probably want to if (!strcmp(arr[n],key)) return n; (the reverse). And the type of array is probably not what you want. Try

const char *str[] = { "mov",.... };

instead. You have an array of arrays of characters and pass it where you actually expect an array of pointers.

这篇关于在字符串数组中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆