By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

char pointers

P: n/a
dms
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

Jul 19 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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.

Jul 19 '05 #2

P: n/a
dms
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.


Jul 19 '05 #3

P: n/a
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.

Jul 19 '05 #4

P: n/a
dms
I can see your point, and appreciate it... but I like to keep my class
declerations within their own .h file and it's functions within their
own .CPP file. The problem with this method is that, if I want to pass a
variable around in this manner, it has to be passed as a pointer as a
global variable cannot be used in this case.

I guess I could make all my current classes subclasses of one giant
class which encapsulates them all, thus eleviating the problem?

All my classes at the moment really are independent of each other in
every respect except for this one variable which needs to be accessed by
all. I am starting to wish I had put them all in one file with a nice
global variable at the top... but that's not me.

Thanks for your help

Dave

Alf P. Steinbach wrote:
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.


Jul 19 '05 #5

P: n/a

"dms" <dm*****@nospam.freeshell.org> wrote in message
news:C1******************@newsfep2-gui.server.ntli.net...
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


Easy

name[2] = 'V';

john
Jul 19 '05 #6

P: n/a
On Wed, 3 Sep 2003 05:40:56 +0100,
John Harrison <jo*************@hotmail.com> wrote:

"dms" <dm*****@nospam.freeshell.org> wrote in message
news:C1******************@newsfep2-gui.server.ntli.net...
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


Easy

name[2] = 'V';


If you like crashing programs...

--
Sam Holden

Jul 19 '05 #7

P: n/a
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
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.


it is simple
memory[index] = x;
you have to check yourself that index is really not crossing the
limits of the memory you allocated otherwise buffer overrun will occur.
Jul 19 '05 #8

P: n/a
> >
Easy

name[2] = 'V';


If you like crashing programs...


If you want to interpret the OP's code literally, which I obviously didn't
do.

Now are you going to explain this to the OP or am a I? I understand what you
are saying but maybe the OP doesn't.

Dear OP, I assumed that you were saying you had a char pointer pointing to a
block of chars. My code would modify one of those chars. However it is not
permitted in C++ to modify string literals. So if your char pointer is
initialised with a string literal then you are not permitted to change that
string by any means. I obviously should have made that clear.

char* name = "David";
name[2] = 'V'; // error

char* name = new char[6];
strcpy(name, "David");
name[2] = 'V'; // OK

john
Jul 19 '05 #9

P: n/a

"Hafiz Abid Qadeer" <ha*************@yahoo.com> wrote in message
news:9c**************************@posting.google.c om...
al***@start.no (Alf P. Steinbach) wrote in message

news:<3f****************@News.CIS.DFN.DE>...
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.


it is simple
memory[index] = x;
you have to check yourself that index is really not crossing the
limits of the memory you allocated otherwise buffer overrun will occur.


This would lead to an access violation.

char *p = "SomeString";
Note that the memory pointed by p is READ only.
You cannot write into it.
It is a liitle misleading but it should be interpreted as
const char *p = "SomeString";

HTH,
J.Schafer

Jul 19 '05 #10

P: n/a
Dave:

I think you may find the following snippet helpful:

// david.cpp
# include <iostream>

void f(char* p)
{
p[2] = 'V';
std::cout << p << '\n';
}

int main()
{
char q[] = "David";
f(q);

char t[] = "David";
char* u = t;
*(u+2) = 'V';
std::cout << u << '\n';
}

Do tell me whether it answered your question.

Regards,
Sumit.


dms <dm*****@nospam.freeshell.org> wrote in message news:<C1******************@newsfep2-gui.server.ntli.net>...
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

Jul 19 '05 #11

P: n/a
On Wed, 3 Sep 2003 06:01:11 +0100,
John Harrison <jo*************@hotmail.com> wrote:
>
> Easy
>
> name[2] = 'V';


If you like crashing programs...


If you want to interpret the OP's code literally, which I obviously didn't
do.

Now are you going to explain this to the OP or am a I? I understand what you
are saying but maybe the OP doesn't.


Since you did, I guess you :)

--
Sam Holden
Jul 19 '05 #12

P: n/a
"Josephine Schafer" <js*@usa.net> wrote in message news:<bj************@ID-192448.news.uni-berlin.de>...
"Hafiz Abid Qadeer" <ha*************@yahoo.com> wrote in message
news:9c**************************@posting.google.c om...
al***@start.no (Alf P. Steinbach) wrote in message

news:<3f****************@News.CIS.DFN.DE>...
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.


it is simple
memory[index] = x;
you have to check yourself that index is really not crossing the
limits of the memory you allocated otherwise buffer overrun will occur.


This would lead to an access violation.

char *p = "SomeString";
Note that the memory pointed by p is READ only.
You cannot write into it.
It is a liitle misleading but it should be interpreted as
const char *p = "SomeString";

HTH,
J.Schafer


You are right. But I thought that he has allocated memory through malloc
or new for this char* and this is not pointing to a constant string.
Jul 19 '05 #13

P: n/a
dms
That's great, thanks.
I must admit, I didn't know that I was pointing to a constant...

Pointers are my biggest week point. I know when I should use them and
then just do trial and error until it works as I don't really understand
them.

Thanks All
Dave

Sumit Rajan wrote:
Dave:

I think you may find the following snippet helpful:

// david.cpp
# include <iostream>

void f(char* p)
{
p[2] = 'V';
std::cout << p << '\n';
}

int main()
{
char q[] = "David";
f(q);

char t[] = "David";
char* u = t;
*(u+2) = 'V';
std::cout << u << '\n';
}

Do tell me whether it answered your question.

Regards,
Sumit.


dms <dm*****@nospam.freeshell.org> wrote in message news:<C1******************@newsfep2-gui.server.ntli.net>...
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


Jul 19 '05 #14

P: n/a
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:

[1] char *name;
[2] 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.


In the above example, "David" is a constant string literal.
Line 2 assigns a pointer to char to the location of the
first character in the constant string literal.

Since this is pointer manipulation, it is still valid in
C++ as well as C. As a reminder (to newbies), the contents
of the string literal _have_not_been_copied_.

Also, because it is a string literal, it cannot be modified
without invoking undefined behavior.

A safer approach is:
char name[] = "David";
The above statement creates an array of characters with
a capacity of 6 (the number of characters in "David" plus
a terminating null), then initializes (i.e. copies) the
array locations with the contents of the string literal.

The safest approach is to use string as Alf has suggested.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #15

P: n/a
> >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.


"David" is a string literal which in the current C++ standard are of
type const char[].

In your code, you declare a pointer to a character and don't
initialize it. You then point the pointer to a string literal. The
compiler reserves memory for all string literals, and your code
shouldn't change this memory, which is what you're trying to do when
you change the 3rd character. Thus, IMHO, you really should do the
following. This really should be at least a compiler warning if you
don't declare it const.

const char * name = "David";

If you want a variable, of type char* that you can change, you need to
do the following.

char * name = new char[strlen("David") + 1];
strcpy(name,"David");

then, you can do this...

name[2] = 'X';

and you have to remember to do this at some point

delete [] name;
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.


Yes, you can do this, however there are times when you may not want to
use std::string, like in an embedded application. Thus, you need to
understand how C style string work as well.
Jul 19 '05 #16

P: n/a
> > 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


Easy

name[2] = 'V';

john


This code is trying to change the contents of a string literal, which
is a bad thing. It's not that "easy".
Jul 19 '05 #17

P: n/a

"Big Brian" <wo**@brianmielke.com> wrote in message
news:d2*************************@posting.google.co m...
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.


"David" is a string literal which in the current C++ standard are of
type const char[].


Actually they are of type char*, otherwise they would not be assignable to a
char* variable.

But they are of course non-modifiable in a correct C++ program.

john
Jul 19 '05 #18

P: n/a
John Harrison wrote:
> >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.
"David" is a string literal which in the current C++ standard are of
type const char[].


Actually they are of type char*, otherwise they would not be assignable to a
char* variable.


No, they are of type 'const char[N]' (see 2.13.4/1). The conversion to
'char*' is provided by a dedicated form of array-to-pointer conversion
introduced specifically for string literals (see 4.2/2).
But they are of course non-modifiable in a correct C++ program.


--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #19

P: n/a

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:vl************@news.supernews.com...
John Harrison wrote:
> >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.

"David" is a string literal which in the current C++ standard are of
type const char[].


Actually they are of type char*, otherwise they would not be assignable to a char* variable.


No, they are of type 'const char[N]' (see 2.13.4/1). The conversion to
'char*' is provided by a dedicated form of array-to-pointer conversion
introduced specifically for string literals (see 4.2/2).
But they are of course non-modifiable in a correct C++ program.


Well they say you learn something new every day. Apologies to Brian, but
being Big I guess he can probably handle it.

john
Jul 19 '05 #20

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message news:bj************@ID-196037.news.uni-

Actually they are of type char*, otherwise they would not be assignable to a
char* variable.


Nope, the standard clearly says in 2.13.4: A string literal has type "array of n const char".

There is special dispensation in the language that allows an implicit conversion to non-const
char for compatibility with a quarter century of sloppy C code.
Jul 19 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.