字符串查询 [英] string query

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

问题描述

我一直在进一步尝试了解订购

字符串。

我更改了测试的代码,订购了一个char列表(其中
但它没有给出我预期的结果。我期待

有越来越多的字符串。


现在,我很困惑,如果有人能解释,我会很感激>
为什么我得到这些结果非常感谢。这是正确的方式吗?

这是正确的方式吗?

struct testRec

{

char name [5];

struct testRec * nextrec;

};


typedef struct testRec records;

typedef records * recordptr;


void addName(recordptr * start,char * item);


int main()

{

recordptr start = NULL;

char item [5];

.......

得到(item);

addRec(& start,item);

.....

返回0;

}


void addRec(recordptr * start,char * item)

{

recordptr newptr,prevptr,thisptr;

newptr = malloc(sizeof(records));


if(newptr!= NULL)

{

strcpy(newptr-> name,item);

newptr-> nextrec = NULL;


prevptr = NULL;

thisptr = * start;


while(thisptr!= NULL)

{

if(item thisptr-> ;姓名)

{

prevptr = thisptr;

thisptr = thisptr-> nextrec;

}

}

if(prevptr == NULL)

{

newptr-> nextrec = * start;

* start = newptr;

}

else

{

prevptr-> ; nextrec = newptr;

newptr-> nextrec = thisptr;

}

}

}


我的输出来自

输入记录:aa

列表是:aa - >

输入记录:dd

列表是:aa -dd - >

输入记录:cc

列表是:aa -dd - cc - >

输入记录:bb

列表是:aa -dd -cc -bd - >

Hi, I''ve been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it''s not giving the results I was expected. I was expecting to
have the strings in an increasing order.

Now, I''m getting confused and would appreciate if someone can explain
why I''m getting these results is greatly appreciated. Am I going about
this the correct way ?

struct testRec
{
char name[5];
struct testRec *nextrec;
};

typedef struct testRec records;
typedef records *recordptr;

void addName(recordptr *start, char *item);

int main ()
{
recordptr start = NULL;
char item[5];
.......
gets(item);
addRec(&start, item);
.....
return 0;
}

void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));

if(newptr != NULL)
{
strcpy(newptr->name, item);
newptr->nextrec = NULL;

prevptr = NULL;
thisptr = *start;

while (thisptr != NULL)
{
if (item thisptr->name)
{
prevptr = thisptr;
thisptr = thisptr->nextrec;
}
}
if (prevptr == NULL)
{
newptr->nextrec = *start;
*start = newptr;
}
else
{
prevptr->nextrec = newptr;
newptr->nextrec = thisptr;
}
}
}

My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -dd ->
Enter a record : cc
List is :aa -dd -cc ->
Enter a record : bb
List is :aa -dd -cc -bd ->

推荐答案

pt*****@gmail.com 说:


< snip>
pt*****@gmail.com said:

<snip>

我的输出为

输入记录:aa

列表是:aa - >

输入a记录:dd

列表是:aa -dd - >

输入记录:cc

列表是:aa -dd -cc - >

输入记录:bb

列表是:aa -dd -cc -bd - >
My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -dd ->
Enter a record : cc
List is :aa -dd -cc ->
Enter a record : bb
List is :aa -dd -cc -bd ->



有多奇怪。我的输出如下:


foo.c:13:警告:函数声明不是原型

foo.c:函数`main'' :

foo.c:14:`NULL''未声明(在此函数中首次使用)

foo.c:14 :(每个未声明的标识符仅报告一次

foo.c:14:对于它出现的每个函数。)

foo.c:16:在'...'之前解析错误

foo.c:18:警告:隐式声明函数`addRec''

foo.c:19:解析前错误'...''

foo .c:21:警告:控制到达无效功能结束

foo.c:顶级:

foo.c:24:警告:之前没有原型对于'addRec''

foo.c:24:警告:类型与先前的隐式声明不匹配

foo.c:18:警告:先前隐式声明`addRec' '

foo.c:24:警告:`addRec''之前已被隐式声明为返回

`int''

foo.c :在函数`addRec中':

foo.c:26:警告:隐式声明函数`malloc''

foo.c:26:警告:赋值使得整数指针没有强制转换

foo.c:28:'NULL''未声明(在此函数中首次使用)

也许发布你实际使用的代码会更好。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

How strange. My output comes as:

foo.c:13: warning: function declaration isn''t a prototype
foo.c: In function `main'':
foo.c:14: `NULL'' undeclared (first use in this function)
foo.c:14: (Each undeclared identifier is reported only once
foo.c:14: for each function it appears in.)
foo.c:16: parse error before `...''
foo.c:18: warning: implicit declaration of function `addRec''
foo.c:19: parse error before `...''
foo.c:21: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:24: warning: no previous prototype for `addRec''
foo.c:24: warning: type mismatch with previous implicit declaration
foo.c:18: warning: previous implicit declaration of `addRec''
foo.c:24: warning: `addRec'' was previously implicitly declared to return
`int''
foo.c: In function `addRec'':
foo.c:26: warning: implicit declaration of function `malloc''
foo.c:26: warning: assignment makes pointer from integer without a cast
foo.c:28: `NULL'' undeclared (first use in this function)
Perhaps it would be better to post the code you''re actually using.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


pt ***** @ gmail.com 写道:


(fx:snip)
pt*****@gmail.com wrote:

(fx:snip)

void addRec(recordptr * start,char * item)
void addRec(recordptr *start, char *item)



(fx:snip)

(fx:snip)


if(item thisptr-> name)
if (item thisptr->name)



指针的比较(`item`和`thisptr-> name`都是

`char *`)不是字符串的比较。更重要的是,这里是

未定义的行为[1]。


(fx:播音员)strcmp先生正在试图与你交朋友。 ..


[1]逃跑!逃跑!


-

这只是我们见过的开始 - Colosseum,/明天'Blues /


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597 England

Comparisons of pointers (`item` and `thisptr->name` are both
`char*`) isn''t comparison of strings. What''s more, here it''s
Undefined Behaviour [1].

(fx:announcer) Mr strcmp is trying to friend you ...

[1] Run away! Run away!

--
"It''s just the beginning we''ve seen" - Colosseum, /Tomorrow''s Blues/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


5月29日上午9:11,ptq2 ... @ gmail.com写道:
On May 29, 9:11 am, ptq2...@gmail.com wrote:

我一直在尝试进一步了解订购

字符串。

我更改了测试的代码,订购了一个char列表(其中

工作)串起来

但它没有给出我预期的结果。我期待

有越来越多的字符串。


现在,我很困惑,如果有人能解释,我会很感激>
为什么我得到这些结果非常感谢。这是正确的方式吗?

这是正确的方式吗?

struct testRec

{

char name [5];

struct testRec * nextrec;


};


typedef struct testRec records;

typedef records * recordptr;
Hi, I''ve been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it''s not giving the results I was expected. I was expecting to
have the strings in an increasing order.

Now, I''m getting confused and would appreciate if someone can explain
why I''m getting these results is greatly appreciated. Am I going about
this the correct way ?

struct testRec
{
char name[5];
struct testRec *nextrec;

};

typedef struct testRec records;
typedef records *recordptr;



[snip]

[snip]


void addRec(recordptr * start,char * item)

{

recordptr newptr,prevptr,thisptr;

newptr = malloc(sizeof(records));


if(newptr!= NULL)

{
void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));

if(newptr != NULL)
{



[snip]

[snip]


while(thisptr!= NULL)

{

if(item thisptr-> name)
while (thisptr != NULL)
{
if (item thisptr->name)



理查德的评论尽管如此,你在测试中遇到问题

以上。


想想看什么你正在比较:

item是一个指针

thisptr-> name是一个字符串(对于这个目的的比较可以被认为是一个指针。

你的目标是确定一个指针在数字上是否比另一个更大?相反,你不关心每个指针指向的字符串之间的关系吗?


重写这个if语句,以便它比较字符串,而不是

指针。当然,请确保您提供(或提供
)原型用于执行任何功能的原型

字符串比较。

Richard''s comments notwithstanding, you have a problem in the test
above.

Think of what you are comparing:
item is a pointer
thisptr->name is a string (which, for the purposes of this
comparison can be considered to be a pointer)
Is it your goal to determine if one pointer is numerically greater
than another? Rather, are you not concerned about the relationship
between the strings pointed to by each pointer?

Rewrite this if statement so that it compares the strings, not the
pointers. And, of course, make certain that you provide (or cause to
be provided) the prototype for any function that you use to perform
that string comparison.


{
{



[snip]


HTH

-

Lew


[snip]

HTH
--
Lew



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

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