字符串到char [英] String to char

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

问题描述

如果我有一个指针,char *。我检查的只有1个字符长,只是使用(char)最好的方式将它转换为char或者是否有另一种方式?

解决方案

Matthew Jakeman< ma ************ @ yahoo.co.uk>潦草地写道:

如果我有一个指针,char *。我检查的只有1个字符长,只是使用(char)最好的方式将它转换为char或者是否有另一种方式?




将其转换为( char)绝对是* WRONG *方式。使用*运算符

来表示它的用途。


char * p;

/ * ... * /

char c;

c = * p;


-

/ - Joona Palaste (pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

你可以选择你的朋友,你可以捡起你的鼻子,但你不能选择你的

亲戚。

- MAD杂志


2003年7月28日星期一12:18:06 -0700

" Mike Wahler" < MK ****** @ mkwahler.net>写道:

Matthew Jakeman< ma ************ @ yahoo.co.uk>在消息中写道
新闻:20030730210038.2ba49ef8.ma ************ @ yahoo。 co.uk ...

如果我有一个指针,char *。



请注意类型为''char *''的对象(指向char),
既不是字符也不是字符串。它可以表示单个字符的地址(类型''char'')
对象。它不能代表一个字符或字符串。

我检查的只有1个字符长,



它''不太可能(但并非不可能)你系统上的指针只有一个字符大小。并且再次注意,此类型只能包含内存位置的*地址*
,而不是字符值。

只是使用(char)将其转换为char



将char *转换为char给出undefined(或者是实现定义的?)行为。
< blockquote class =post_quotes>最好的方式还是有另一种方式?



最好的办法是什么?

如果你有一个类型的指针' 'char *''包含一个字符对象的
地址,或者一个字符数组的第一个地址,你可以访问''指向''
字符解引用算子:

char some_stuff [] =" Hello";
char * p;
char c;

p =& some_stuff [ 1];
c = * p; / *对象''''现在包含字符''e''* /

你读的C书是什么,不能解释指针?

-Mike





在我的位置消息,我说我一直在阅读C书/书籍,解释指针?我没有,我请求帮助,感谢那些设法提出一个简单问题的段落,这些段落明显与你的智力相比显得相形见绌,变成了一个非常复杂的东西,但你面前的回复很多更少的贬低和更多的帮助。




Matthew Jakeman< ma ******* *****@yahoo.co.uk>在消息中写道

news:20030730212931.42e9d2a1.ma ************ @ yahoo。 co.uk ...

2003年7月28日星期一12:18:06 -0700
Mike Wahler < MK ****** @ mkwahler.net>写道:

Matthew Jakeman< ma ************ @ yahoo.co.uk>在消息中写道
新闻:20030730210038.2ba49ef8.ma ************ @ yahoo。 co.uk ...

如果我有一个指针,char *。
请注意,类型为''char *''的对象(指向char的指针),
是既不是字符也不是字符串。它可以表示单个字符的地址(类型''char'')
对象。它不能代表一个字符或字符串。

我检查的只有1个字符长,



它''不太可能(但并非不可能)你系统上的指针只有一个字符大小。并且再次注意,此类型只能包含内存位置的*地址*
,而不是字符值。

只是使用(char)将其转换为char



将char *转换为char给出undefined(或者是实现定义的?)行为。
< blockquote class =post_quotes>最好的方式还是有另一种方式?



最好的办法是什么?

如果你有一个类型的指针' 'char *''包含一个字符对象的
地址,或者一个字符数组的第一个地址,你可以访问''指向''
字符解引用算子:

char some_stuff [] =" Hello";
char * p;
char c;

p =& some_stuff [ 1];
c = * p; / *对象''''现在包含字符''e''* /

你读的C书是什么,不能解释指针?

-Mike




在我的消息中我说的是什么我一直在读C书/书



解释指针?


你没有。但是,你只是浪费你的时间

试图在没有教科书的情况下学习C语言。通过向usenet发布问题,你无法学习

''零碎'。


我没有,我求救,


这里给出的帮助是为了补充而不是替换,更多传统的学习材料,如书籍。

谢谢设法提出一个简单问题的段落,


您的问题不仅简单,而且非常模糊,

并指出了我试图纠正的错误观念。

来自显然与你的智力相形见绌的人,


令人讨厌的讽刺不会得到你在这里很远。

变成了一个非常复杂的东西



我不认为我对一个简单主题的任何解释

很复杂。如果你发现它,那么你真的需要一些书。

但你面前的回复要少得多贬值



请指出在我的回复中我使用了任何

''demeaning''文本。我所做的一切都是陈述事实。

如果你对这些事实感到不满,请把它拿出来给他们带来


和地狱更多帮助。




它可能是,它可能不是。 Joona不得不猜测你真正想要的是什么。他还显然选择了

来忽略你发布的部分内容,这些部分表明了你对C的误解。


-Mike


If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?

解决方案

Matthew Jakeman <ma************@yahoo.co.uk> scribbled the following:

If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?



Casting it to (char) is definitely the *WRONG* way. Use the * operator
for what it is meant for.

char *p;
/* ... */
char c;
c = *p;

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You can pick your friends, you can pick your nose, but you can''t pick your
relatives."
- MAD Magazine


On Mon, 28 Jul 2003 12:18:06 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:

Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730210038.2ba49ef8.ma************@yahoo. co.uk...

If i have a pointer, char *.



Note that an object of type ''char*'' (pointer to char),
is neither a character nor a string. It can represent
the address of an indivudual character (type ''char'')
object. It cannot represent a character or a string.

That i have checked is only 1 character long,



It''s very unlikely (but not impossible) that a pointer
on your system is only one character in size. And
again, note that this type can only contain an *address*
of a memory location, not a character value.

is just casting it to a char using (char)



Casting a char* to a char gives undefined (or is it
implementation-defined?) behavior.

the best way or is there another way ?



Best way to do what?

If you have a pointer of type ''char*'' which contains the
address of a character object, or the address of the first
of an array of characters, you access the ''pointed to''
character with the dereference operator:

char some_stuff[] = "Hello";
char *p;
char c;

p = &some_stuff[1];
c = *p; /* The object ''c'' now contains the character ''e'' */

What C book(s) are you reading that don''t explain
pointers?

-Mike




At what point in my message did i say i had been reading a C book / books that explain pointers ? I didn''t, i asked for help, thanks for the paragraphs that have managed to make a simple question, from someone who is obviously dwarfed by your mass of intelligence, turn into something immensely more complex but the reply before yours was a lot less demeaning and a hell of a lot more helpful.



Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730212931.42e9d2a1.ma************@yahoo. co.uk...

On Mon, 28 Jul 2003 12:18:06 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:

Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730210038.2ba49ef8.ma************@yahoo. co.uk...

If i have a pointer, char *.
Note that an object of type ''char*'' (pointer to char),
is neither a character nor a string. It can represent
the address of an indivudual character (type ''char'')
object. It cannot represent a character or a string.

That i have checked is only 1 character long,



It''s very unlikely (but not impossible) that a pointer
on your system is only one character in size. And
again, note that this type can only contain an *address*
of a memory location, not a character value.

is just casting it to a char using (char)



Casting a char* to a char gives undefined (or is it
implementation-defined?) behavior.

the best way or is there another way ?



Best way to do what?

If you have a pointer of type ''char*'' which contains the
address of a character object, or the address of the first
of an array of characters, you access the ''pointed to''
character with the dereference operator:

char some_stuff[] = "Hello";
char *p;
char c;

p = &some_stuff[1];
c = *p; /* The object ''c'' now contains the character ''e'' */

What C book(s) are you reading that don''t explain
pointers?

-Mike




At what point in my message did i say i had been reading a C book / books


that explain pointers ?

You didn''t. But imo you''re only wasting your time
trying to learn C without textbooks. You can''t learn
it ''piecemeal'' by posting questions to usenet.

I didn''t, i asked for help,
Help given here is intended to supplement, not replace,
the more traditional learning materials, such as books.
thanks for the paragraphs that have managed to make a simple question,
Your question wasn''t only ''simple'', but it was quite vague,
and indicated misconceptions, which I attempted to rectify.
from someone who is obviously dwarfed by your mass of intelligence,
Obnoxious sarcasm won''t get you very far here.
turn into something immensely more complex

I don''t think any of my explanation of a simple topic
was complex at all. If you find it so, then you really
do need some books.
but the reply before yours was a lot less demeaning

Please point out where in my reply I used any
''demeaning'' text. All I did was state facts.
If you''re upset with those facts, take it up
with them.
and a hell of a lot more helpful.



It might be, it might not. Joona had to guess at
what you really wanted. He also apparently chose
to overlook the parts of your post that indicate
your misconceptions about C.

-Mike


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

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