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

Discussion in 'C++' started by cylin, Jul 18, 2003.

  1. cylin

    cylin Guest

    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.
     
    cylin, Jul 18, 2003
    #1
    1. Advertising

  2. "> 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
     
    Josephine Schafer, Jul 18, 2003
    #2
    1. Advertising

  3. "cylin" <> wrote in message
    news:bf8d3v$c6p3f$-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)
     
    Jakob Bieling, Jul 18, 2003
    #3
  4. cylin

    Kevin Saff Guest

    "cylin" <> wrote in message
    news:bf8d3v$c6p3f$-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.
     
    Kevin Saff, Jul 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhijit Bhadra
    Replies:
    2
    Views:
    7,476
    Ron Natalie
    Dec 1, 2004
  2. Pierre McCann

    comparing pointed-at value with another value

    Pierre McCann, Oct 10, 2003, in forum: C Programming
    Replies:
    6
    Views:
    358
    Mark McIntyre
    Oct 10, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  4. Disc Magnet
    Replies:
    1
    Views:
    639
    Ian Collins
    May 6, 2010
  5. tahir rauf
    Replies:
    12
    Views:
    744
    Keith Thompson
    Apr 30, 2011
Loading...

Share This Page