将char *与字符串文字进行比较时出现混淆

编程入门 行业动态 更新时间:2024-10-24 02:37:04
本文介绍了将char *与字符串文字进行比较时出现混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include< stdio.h> int main() { char * str = NULL; char x [] ="今天很好!" ;; printf("%s",str); str = strtok(x,""); if(str ==" today")//< ==这里是令我困惑的行 printf (他们等于!\ n); 返回0; } 我打印了str首先,控制台显示今天。但是,当我尝试用'今天'与'str''合作时, ,条件失败了! 确切地说,我知道''' str''是char类型的4字节指针, 因此它不等于字符串。但即使在gdb中,我也使用''p str'', 打印出来的今天。 所以,我的问题是我们怎么做比较数组和char *(我发现它 常用作字符串)? 还有一个问题:我们必须初始化一个char *或 将它指向内存中的某个地方,或者在我们 可以使用它之前将它指向malloc内存? 因为我得到了这个问题也引发了几次段错。 提前感谢您的回复! Ji

解决方案

Dnia Sat,2007年3月17日12:01:03 -0700,william napisa3(a):

#include< stdio.h>

在C ++中它是#include< cstdio>

int main() { char * str = NULL; char x [] ="今天很好!" ;; printf (%s,str);

您正在尝试打印由空指针指向的内容 [指向无处的指针]。在C ++中访问空指针 是UB。它可能会崩溃,它是我的打印(null),它可能打印 一些垃圾或什么都不做。

str = strtok(x,"");

现在你要设置指向 strtok找到的第一个令牌。直到现在它并没有指向任何地方。它指向 a开头的今天就好了!\ 0...... [strtok在第一个令牌今天之后放了 NUL字符。 ]。

if(str ==" today")//< ==这里是令我困惑的行 printf( 他们等于!\ n;); 返回0; } 我打印了str首先,控制台显示今天。

对我而言,它显示(null)。这是真的,因为传递给第一个printf的 ''str'指针设置为无处。

但是,当我尝试用'今天'comapare''str''时, 条件失败了!

这是正确的行为,因为你在比较 指针[不是字符串],它们指向不同的记忆 位置;)所以他们不相等。

确切地说,我知道''str''是4字节指针

在某些平台上;)

的char类型,所以它不等于a串。

什么字符串?你的意思是字面常量吗?

但是即使在gdb中,我使用了''p str'',它打印了今天。

它也没问题,因为''str''指向一个记忆位置, 其中今天下蛋。但它今天并不相同。作为你比较的那个; P第一个今天是 数组x的一部分,第二个是文字常量今天。在等式表达式中使用 。那两个今天的内存位置不一样,即使它们包含相同的序列 的字符。

所以,我的问题是我们如何比较数组和 char *

strcpy()或strncpy ();)

(我发现它是如此常用的字符串)?

不,它通常用于POINT AT字符串/数组。

还有一个问题:我们是否必须初始化 a char *或将其指向内存中的某个地方

是的。特别是对于自动[local]对象,因为它们是默认的零初始化并且包含垃圾[对于 指针:它们是针对随机存储器的位置]。

或者我们可以使用它之前的malloc内存吗?

是的,它们应该指向已分配的内存 - 通过 new / malloc(),或由编译器本身[已定义]分配。

因为我有多次因为 而产生段错误这个问题。

阅读一些关于指针和字符串文字的内容。 - SasQ

嘿 你要将常量stinf与char *进行比较。首先你需要 进行打字。 例子 String s =" today" if(str ==(char *)s.c_str()) 但我认为你不能使用==与char *。你可以使用c ++字符串 变量。 所以你可以这样做 if(strcmp(str,(char) *)s.c_str())== 0) 纠正我,如果我错了:) int main() { char * str = NULL; char x [] ="今天很好!" ;; char * s =" today"; printf("%s",str); str = strtok(x,""); if(strcmp(str,s)== 0)//< ==这里是让我困惑的行 printf("等于!\ n"); getch(); 返回0; } 谢谢 sanjay 3月17日下午1:23,SasQ< s ... @ go2.plwrote:

Dnia Sat,2007年3月17日12:01:03 -0700,william napisa3(a):

#include< stdio.h>

在C ++中它是#include< cstdio>

int main() { char * str = NULL; char x [] ="今天很好!" ;; printf (%s,str);

你正在尝试打印由空指针指向的东西 [指向无处的指针]。在C ++中访问空指针 是UB。它可能会崩溃,它是我的打印(null),它可能打印 一些垃圾或什么都不做。

str = strtok(x,"");

现在你要设置指向 strtok找到的第一个令牌的指针。直到现在它并没有指向任何地方。它指向 a开头的今天就好了!\ 0...... [strtok在第一个令牌今天之后放了 NUL字符。 ]。

if(str ==" today")//< ==这里是令我困惑的行 printf( 他们等于!\ n;); 返回0; }

我打印了str首先,控制台显示今天。

对我而言,它显示(null)。这是真的,因为传递给第一个printf的 ''str'指针设置为无处。

但是,当我尝试用'今天'comapare''str''时, 条件失败了!

它是正确的行为,因为你在比较 指针[不是字符串],它们指向不同的记忆 位置;)所以他们不相等。

确切地说,我知道''str''是4字节指针

在某些平台上;)

的char类型,所以它不等于a串。

什么字符串?你的意思是字面常量吗?

但是即使在gdb中,我使用了''p str'',它打印了今天。

它也没关系,因为''str''指向一个记忆位置, 其中今天下蛋。但它今天并不相同。作为你比较的那个; P第一个今天是 数组x的一部分,第二个是文字常量今天。在等式表达式中使用 。那两个今天的内存位置不一样,即使它们包含相同的序列 的字符。

所以,我的问题是我们如何比较数组和 char *

strcpy()或strncpy ();)

(我发现它是如此常用的字符串)?

不,它通常用于POINT AT字符串/数组。

还有一个问题:我们是否必须初始化 a char *或将其指向内存中的某个地方

是的。特别是对于自动[local]对象,因为它们是默认的零初始化并且包含垃圾[对于 指针:它们是针对随机存储器的位置]。

或者我们可以使用它之前的malloc内存吗?

是的,它们应该指向已分配的内存 - 或者通过 new / malloc(),或者由编译器本身[已定义]分配。

因为我有多次因为 而产生段错误这个问题。

阅读一些关于指针和字符串文字的内容。 - SasQ

Dnia星期六,2007年3月17日21:23:04 +0100,SasQ napisa3(a): 抱歉,有点勘误:

现在你要设置指向 strtok找到的第一个令牌的指针。直到现在它并没有指向任何地方。

应该是从现在开始而不是直到现在。 [英语不是我的母语; P]

>所以,我的问题是我们如何比较数组和char *

strcpy()或strncpy();)

应该是strcmp()或strncmp() - SasQ

#include <stdio.h> int main() { char *str=NULL; char x[]="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n"); return 0; } I printed "str" first, and the console displayed "today". However, when I try to comapare ''str'' with "today", the condition failed! Exactly speaking, I know that ''str'' is a 4 byte pointer of char type, so it is not equal to a string. But even in the gdb, I used ''p str'', it printed "today". So, my question is how do we compare arrays and char *(I found that it is so commonly used as string)? one more question: is it true that we have to initialize a char * or points it to somewhere in memory, or to malloc memory to it before we can use it? Because I got segfault several times arising from this problem too. Thank you for your reply in advance! Ji

解决方案

Dnia Sat, 17 Mar 2007 12:01:03 -0700, william napisa3(a):

#include <stdio.h>

In C++ it is #include <cstdio>

int main() { char *str=NULL; char x[]="today is good!"; printf("%s", str);

You''re trying to print something pointed by a null pointer [a pointer that points to "nowhere"]. Accessing a null pointer in C++ is UB. It may crash, it my print "(null)", it may print some garbage or do nothing.

str=strtok(x," ");

Now you''re setting the pointer to the first token found by strtok. It doesn''t point to nowhere until now. It points to a beginning of "today\0is good!\0"... [the strtok put the NUL character after first token "today"].

if (str=="today") //<==here is line that confuses me printf("they equals!\n"); return 0; } I printed "str" first, and the console displayed "today".

For me it displays "(null)". And it''s true, because the ''str'' pointer passed to the first printf is set to nowhere.

However, when I try to comapare ''str'' with "today", the condition failed!

And it''s proper behaviour, because you''re comparing the pointers [not strings], and they point to different memory locations ;) So they''re not equal.

Exactly speaking, I know that ''str'' is a 4 byte pointer

On some platforms ;)

of char type, so it is not equal to a string.

What string? Do you mean that literal constant?

But even in the gdb, I used ''p str'', it printed "today".

And it''s also OK, because ''str'' points to a memory location, where "today" lays. But it''s not the same "today" as the one you compare with ;P The first "today" is the part of the array x, and the second is the literal constant "today" used in equality expression. That two "today"''s are not the same memory locations, even if they contain the same sequences of characters.

So, my question is how do we compare arrays and char *

strcpy() or strncpy() ;)

(I found that it is so commonly used as string)?

No, it''s commonly used to POINT AT character strings/arrays.

one more question: is it true that we have to initialize a char * or points it to somewhere in memory

Yes. Especially for automatic [local] objects, because they''re not zero-initialized by default and contain garbage [for pointers: they''re aimed at random memory locations].

or to malloc memory to it before we can use it?

Yes, they should point to allocated memory - either by new/malloc(), or allocated by compiler itself [defined].

Because I got segfault several times arising from this problem too.

Read some about pointers and string literals. -- SasQ

hey you tring to compare constant stinf to char *. first of all you need to do type casting. example String s = "today" if ( str == (char *)s.c_str()) but i think you cant use "==" with char *. you can use with c++ string variable. so u could do like this if ( strcmp(str,(char *)s.c_str()) == 0) correct me if i am wrong :) int main() { char *str=NULL; char x[]="today is good!"; char *s="today"; printf("%s", str); str=strtok(x," "); if (strcmp(str,s) == 0) //<==here is line that confuses me printf("they equals!\n"); getch(); return 0; } thanks sanjay On Mar 17, 1:23 pm, SasQ <s...@go2.plwrote:

Dnia Sat, 17 Mar 2007 12:01:03 -0700, william napisa3(a):

#include <stdio.h>

In C++ it is #include <cstdio>

int main() { char *str=NULL; char x[]="today is good!"; printf("%s", str);

You''re trying to print something pointed by a null pointer [a pointer that points to "nowhere"]. Accessing a null pointer in C++ is UB. It may crash, it my print "(null)", it may print some garbage or do nothing.

str=strtok(x," ");

Now you''re setting the pointer to the first token found by strtok. It doesn''t point to nowhere until now. It points to a beginning of "today\0is good!\0"... [the strtok put the NUL character after first token "today"].

if (str=="today") //<==here is line that confuses me printf("they equals!\n"); return 0; }

I printed "str" first, and the console displayed "today".

For me it displays "(null)". And it''s true, because the ''str'' pointer passed to the first printf is set to nowhere.

However, when I try to comapare ''str'' with "today", the condition failed!

And it''s proper behaviour, because you''re comparing the pointers [not strings], and they point to different memory locations ;) So they''re not equal.

Exactly speaking, I know that ''str'' is a 4 byte pointer

On some platforms ;)

of char type, so it is not equal to a string.

What string? Do you mean that literal constant?

But even in the gdb, I used ''p str'', it printed "today".

And it''s also OK, because ''str'' points to a memory location, where "today" lays. But it''s not the same "today" as the one you compare with ;P The first "today" is the part of the array x, and the second is the literal constant "today" used in equality expression. That two "today"''s are not the same memory locations, even if they contain the same sequences of characters.

So, my question is how do we compare arrays and char *

strcpy() or strncpy() ;)

(I found that it is so commonly used as string)?

No, it''s commonly used to POINT AT character strings/arrays.

one more question: is it true that we have to initialize a char * or points it to somewhere in memory

Yes. Especially for automatic [local] objects, because they''re not zero-initialized by default and contain garbage [for pointers: they''re aimed at random memory locations].

or to malloc memory to it before we can use it?

Yes, they should point to allocated memory - either by new/malloc(), or allocated by compiler itself [defined].

Because I got segfault several times arising from this problem too.

Read some about pointers and string literals. -- SasQ

Dnia Sat, 17 Mar 2007 21:23:04 +0100, SasQ napisa3(a): Sorry, a little errata:

Now you''re setting the pointer to the first token found by strtok. It doesn''t point to nowhere until now.

Should be "from now on" instead of "until now". [English isn''t my native language ;P]

>So, my question is how do we compare arrays and char *

strcpy() or strncpy() ;)

Should be "strcmp() or strncmp()" -- SasQ

更多推荐

将char *与字符串文字进行比较时出现混淆

本文发布于:2023-07-28 14:13:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文字   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!