473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
Jul 19 '05 #1
3 3239

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

--
With best wishes,
J.Schafer
Jul 19 '05 #2
"cylin" <cy***@avant.com.tw> wrote in message
news:bf************@ID-154203.news.uni-berlin.de...
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;
}
---------------------------------------------------
Why does *test still equal to NULL?
'*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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #3
"cylin" <cy***@avant.com.tw> wrote in message
news:bf************@ID-154203.news.uni-berlin.de...
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,
-----------------------------------------------------
#include <iostream>
using namespace std;

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.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gsyoon | last post by:
hi, all. I'm trying to make a "framework" to store the return value of a function to a global memory. My first attempt was 1) void store_to_global( char * type_name ) { if ( strcmp(...
2
by: Sara Shoemaker | last post by:
Hi all - I am trying to launch a web browser from within my code for Linux. The problem is that I am getting no failure code back when the browser doesn't launch. (I'm trying to force a failure...
3
by: Don Pasquale | last post by:
The following function intends to delete "numberoflines" lines from a text file, named "s" (string pointer) and pointed to by file pointer "fp", starting from line "line". Now, the function...
4
by: jrefactors | last post by:
In the following program, are parameters s in function reverse() and x in function count() both pass by value? How come value k is not changed, but value str has changed? Please advise....
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
4
by: arnuld | last post by:
1.) we usually say /return 0/ indicates success e.g #include <iostream> int main() { int i=3; std::cout << i << std::endl; return 0;
12
by: Frodo Baggins | last post by:
Hi I need to know the size of the memory block pointed to by a char* in a function receiving this pointer. Typically, this pointer points to a string. strlen() will not do the job since sometimes...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
10
by: somenath | last post by:
Hi All, I was trying to understand the pass by value mechanism in c. To understand it I wrote a Small code as mentioned bellow. #include<stdio.h> #include<stdlib.h> #include<string.h> void...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.