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 20 8803
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.
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.
"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
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 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.
> > 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
"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
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
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
"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.
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
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
> >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.
> > 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".
"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
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
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Espen Ruud Schultz |
last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string's "content" so that the char pointer can point to
the same text? And vice versa; can I give...
|
by: Aaron Walker |
last post by:
I was just wondering, if I have:
struct my_struct {
char *str1;
char *str2;
};
and then elsewhere do:
struct my_struct *ms = calloc(1, sizeof(struct my_struct));
|
by: gaga |
last post by:
I can't seem to get this to work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names;
char **np;
|
by: Frederick Gotham |
last post by:
I think someone posted elsewhere on the group that we must be able to
address at least 65 535 bytes in C... ? (Or something along those lines).
A 16-Bit unsigned integer type has 65 536 unique...
|
by: newbie |
last post by:
Can someone please give me some tips?
I have a function f(). In function f() I allocate memory for a char
array. Now the memory I allocate in f() must be assigned to a char*
outside of f() so...
|
by: ziroi |
last post by:
Hello!
I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
|
by: dtschoepe |
last post by:
Hi,
I am working on a project for school and I am trying to get my head
around something weird. I have a function setup which is used to get
an input from stdin and a piece of memory is created...
|
by: mdh |
last post by:
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1, int limit);
int main () {
f();
return 0;
}
|
by: krreks |
last post by:
Hi.
I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet......
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |