Thread: Newbie char pointer problem

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001

    Newbie char pointer problem

    I have a function like this:
    void stringchange(char* sometext) {
      char* newtext = "thisisnewtext";
      /*Some code to change content of sometext array
         to content of newtext array*/
    int main() {
      char* mytext;
      cin >> mytext;
      stringchange(mytext);  //???
      return 0;
    My question is, how should I send mytext char array pointer to stringchange function so it can change sometext array to content of newtext array? And what code should be inside this function to perform thus assignment? Length of mytext array is not defined at the compile-time. I want to change mytext array the same way as sometext array does, of course!
    Please excuse my poor english...

  2. #2
    Join Date
    Aug 2001
    It's going to segfault as soon as anyone enters
    anything as mytex points to random value.

    If you want to change what mytext points
    to then the protype is fine. If your going to
    be dynamically allocating the string and hence changing
    mytext then you will have to use

    void stringchange(char** sometext)

  3. #3
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Please excuse my poor english...

  4. #4
    Registered User
    Join Date
    Sep 2001
    Nick, I don't see how a pointer to a pointer solves his problem.

    First off, he is using a pointer without initializing it. You will get a warning, and it will allow you to compile, but that is not a good practice.

    Secondly, it is also a good practice to allocate the space needed for the character array. If the string is very long, it may overwrite other variables in his program.

    Thirdly, cin will only accept input up until the first space. Anything after that will be left in the keyboard buffer.

    if he does not know how much room he will need, then he should allocate some character buffer space and if he needs more, allocate more.

    char MyBuffer[250];
    char *pMyBuffer;

    pMyBuffer = MyBuffer

    if he finds out that he needs more, I would allocate more with the malloc function call.

    I would change cin to getche, and place it in a while loop looking for the enter key. I would then add to my buffer the result from getche. I would also keep ckecking in the while loop the size of the buffer to see if I needed to allocate more space. I would allocate space by 250 characters each time.

    He could them pass the pointer *pMyBuffer to his function stringchange(pMyBuffer) and do whatever he wants with the character data.

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Thanx again for you help guys. I'm new to C++. I've learnt how to use "new" keyword to dynamically allocate memory for strings with at-the-compile-time-unknown length recently. So I got no problems with char arrays now.
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer problem
    By l2u in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 05:18 PM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. C++ (char *) problem?
    By Teegtahn in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 01:23 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM