char pointers

D

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
 
A

Alf P. Steinbach

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.
 
D

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?
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.
 
A

Alf P. Steinbach

[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.
 
D

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
[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.
 
J

John Harrison

dms said:
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
 
S

Sam Holden

dms said:
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...
 
H

Hafiz Abid Qadeer

[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.
 
J

John Harrison

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
 
J

Josephine Schafer

Hafiz Abid Qadeer said:
(e-mail address removed) (Alf P. Steinbach) wrote in message
[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
 
S

Sumit Rajan

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.
 
H

Hafiz Abid Qadeer

Josephine Schafer said:
Hafiz Abid Qadeer said:
(e-mail address removed) (Alf P. Steinbach) wrote in message
[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.
 
D

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 said:
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 said:
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
 
T

Thomas Matthews

Alf said:
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
 
B

Big Brian

char *name;
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.
 
B

Big Brian

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".
 
J

John Harrison

Big Brian said:
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
 
A

Andrey Tarasevich

John said:
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).
 
J

John Harrison

Andrey Tarasevich said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top