char指针 [英] char pointers

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

问题描述

大家好,


如果你有一个char指针(char * var),有没有办法可以操作

特定的字符或块字符串中的字符?


例如:


char * name;

name =" David"

然后将第3个字符更改为V,现在字符串显示为:


DaVid?


感谢您的帮助


Dave

解决方案

2003年9月3日星期三03:06:55 + 0100,dms< dm ***** @ nospam.freeshell.org>写道:

如果你有一个字符指针(char * var),有没有办法可以操作字符串中的特定字符或字符块?


我的字符串指针与你的字符串有什么关系?

例如:

char * name;
name =" David"
然后将第3个字符更改为V,这样字符串现在显示为:




如果我没记错的话(有人纠正我的话)C ++仍然_allows_

这是一个特殊情况,为了向后兼容C.


但是David是一个常数,你永远不应该尝试改变

常数。


相反,这样做:


#include< string>


...


std :: string name =" David" ;;


name [2] =''D'';

在这种情况下,David被复制到一个变量中,你可以改变变量的内容。


Hth。


基本上,这个例子只是一个更复杂的

程序的基本例子,但这就是问题的主旨。


我有多个班级,彼此独立,但他们沟通的唯一方式是通过


char *记忆。

我用每个类中的函数调用& memory作为参数。我怎么用
更改这个数组中的个别字符?


Alf P. Steinbach写道:

2003年9月3日星期三03:06:55 +0100,dms< dm ***** @ nospam.freeshell.org>写道:

如果你有一个char指针(char * var),有没有办法可以操作字符串中的特定字符或字符块?



我的字符串指针与你的字符串有什么关系?

例如:

char * name;
name =" David"
然后将第3个字符更改为V,这样字符串现在显示为:



如果我没记错的话(某人正确)我,如果没有)C ++仍然_allows_
这是一个特殊情况,为了向后兼容C.

但是大卫是一个常数,你永远不应该试图改变一个
常数。

相反,这样做:

#include< string>

...

std :: string name =" David" ;;

name [2] =''D'';

在这种情况下,大卫被复制到一个变量中,你可以改变变量的内容。

Hth。




< blockquote> 2003年9月3日星期三03:21:06 +0100,dms< dm ***** @ nospam.freeshell.org>写道:


[请不要发帖 - 下次没有回答]


我有多个班级,彼此独立,但是他们沟通的唯一方式是通过内存来实现。

我用每个类中的函数调用& memory作为参数。


你到处传递的参数表示需要

一个班级。


而不是将该参数传递给使用它的函数和

操作它,让它成为对象的内部状态,使用成员

函数使用它并操作在它上面。


另外,如前一篇文章中所述,如果它的确是一个字符串,那么使用一个std :: string,但也许还有更多吗?

如何更改此数组中的单个字符?




请参阅上一篇文章。


Hi all,

If you have a char pointer (char *var), is there a way I can manipulate
specific characters or blocks of characters within the string?

eg:

char *name;
name = "David"
then change the 3rd character to V so the string now reads :

DaVid?

Thanks for your help

Dave

解决方案

On Wed, 03 Sep 2003 03:06:55 +0100, dms <dm*****@nospam.freeshell.org> wrote:

If you have a char pointer (char *var), is there a way I can manipulate
specific characters or blocks of characters within the string?
What has my char pointer to do with your string?
eg:

char *name;
name = "David"
then change the 3rd character to V so the string now reads :



If I remember correctly (somebody correct me if not) C++ still _allows_
this as a special case, for backward compatibility with C.

But "David" is a a constant, and you should never try to change a
constant.

Instead, do this:

#include <string>

...

std::string name = "David";

name[2] = ''D'';
In this case "David" is copied into a variable, and you can change
the contents of variable.

Hth.


Basically, the example was just a basic example for a more complex
program, but that was the jist of the problem.

I have multiple classes, all independent of each other, but the only way
they communicate is through

char *memory.

I call functions in each class with &memory as a parameter. How do I
change individual characters within this array?

Alf P. Steinbach wrote:

On Wed, 03 Sep 2003 03:06:55 +0100, dms <dm*****@nospam.freeshell.org> wrote:

If you have a char pointer (char *var), is there a way I can manipulate
specific characters or blocks of characters within the string?


What has my char pointer to do with your string?

eg:

char *name;
name = "David"
then change the 3rd character to V so the string now reads :


If I remember correctly (somebody correct me if not) C++ still _allows_
this as a special case, for backward compatibility with C.

But "David" is a a constant, and you should never try to change a
constant.

Instead, do this:

#include <string>

...

std::string name = "David";

name[2] = ''D'';
In this case "David" is copied into a variable, and you can change
the contents of variable.

Hth.




On Wed, 03 Sep 2003 03:21:06 +0100, dms <dm*****@nospam.freeshell.org> wrote:

[Do not top-post -- next time no answer]


I have multiple classes, all independent of each other, but the only way
they communicate is through

char *memory.

I call functions in each class with &memory as a parameter.
A parameter you''re passing around everywhere is indicative of the need
for a class.

Instead of passing that parameter around to functions that use it and
operate on it, let it be the internal state of an object, with member
functions that use it and operate on it.

Also, as mentioned in the previous posting, use a std::string if it''s
really a string -- but perhaps there is more to it than that?
How do I change individual characters within this array?



See the previous posting.


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

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