How to change the the value which is pointed by a char pointer by a function?

C

cylin

Dear all,

I met a char pointer problem. please help,thanks.
How to change the the value which is pointed by a char pointer by a
function?

for example,
-----------------------------------------------------
#include <iostream>
using namespace std;

void addstr(char *str,int len) {
str=new char[len];
for (int i=0;i<(len-1);i++)
{
*(str+i)=char(65+2*i);
}
}

int main()
{
char *test;
int len=10;
addstr(test,len);
for (int i=0;*(test+i);i++)
cout << "here=" << *(test+i);
cout << endl;
}
---------------------------------------------------
Why does *test still equal to NULL?
How do I fix the function addstr so that test=str?

Best Regards,
cylin.
 
J

Josephine Schafer

"> for example,
-----------------------------------------------------
#include <iostream>
using namespace std;

void addstr(char *str,int len) {

Change the above line to void addstr(char *& str,int len) {
str=new char[len];
for (int i=0;i<(len-1);i++)
{
*(str+i)=char(65+2*i);
}
}

int main()
{
char *test;
int len=10;
addstr(test,len);
for (int i=0;*(test+i);i++)
cout << "here=" << *(test+i);
cout << endl;
}

It should work now.
 
J

Jakob Bieling

cylin said:
Dear all,

I met a char pointer problem. please help,thanks.
How to change the the value which is pointed by a char pointer by a
function?

for example,
-----------------------------------------------------
#include <iostream>
using namespace std;

void addstr(char *str,int len) {

'str' is of type char*. Write it like this to make it more obvious:

void addstr(char* str,int len)
str=new char[len];

Here you are just changing the local variable 'str'. See below.
for (int i=0;i<(len-1);i++)
{
*(str+i)=char(65+2*i);
}
}

int main()
{
char *test;
int len=10;
addstr(test,len);

Above I said 'str' is of type 'char*'. 'test' is of type 'char*' as
well. So what happens here is, you create a /copy/ the value of 'test' (the
uninitialized pointer value (*)). This means that if 'addstr' modifies it,
it actually only modifies the copy and your 'test' variable is left
unchangd. That is why you do not want the function 'addstr' to have a copy
of the pointer, you want it to be able to modify the pointer itself, so you
either need to pass a pointer to the pointer, or a reference to the pointer.
for (int i=0;*(test+i);i++)
cout << "here=" << *(test+i);
cout << endl;
}

'*test' is not NULL. '*test' is undefined, just like 'test' (the pointer
value itself) has an unknown value.
How do I fix the function addstr so that test=str?

Pass the pointer value by reference:

void addstr (char*& str, int len);


To someone with more knowledge of the Standard:

(*) Is the following legal?

char* ptr;
char* ptr2 = ptr;

? Because I remember a discussion (in comp.lang.c++.moderated maybe?)
Where it was said that a pointer with an invalid value may not even be read.

regards
 
K

Kevin Saff

cylin said:
Dear all,

I met a char pointer problem. please help,thanks.
How to change the the value which is pointed by a char pointer by a
function?

Other people seemed to answer this question, but other parts of this code
were fishy, so I thought I'd add 2c.
for example,

void addstr(char * & str,int len) { // as others suggested
str=new char[len];
for (int i=0;i<(len-1);i++)
{
*(str+i)=char(65+2*i);
}

*(str + len - 1) = 0; // remember to terminate string!

Maybe I'm missing something, but you need to make sure to actually set the
last character to 0; if you don't, you could get 0xDEADBEEF.
int main()
{
char *test;
int len=10;
addstr(test,len);
for (int i=0;*(test+i);i++)
cout << "here=" << *(test+i);
cout << endl;
}

Consider incrementing pointers instead of integers for this type of thing;
the convenience of doing so is one reason zero-terminated strings were
popular for so long.

char *c = test;
while (*c)
cout << "here=" << *c++;

Better yet, use std::string if at all possible; it's cleaner and safer.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top