Thread: Pointer

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    1
    Rep Power
    0

    Pointer


    #define MAXLEN 50
    char *pMsgString = {"HDLC defines three different data.......................................... "};
    void split(char *pMessage)
    {
    int slen, leng;

    char *pE, *pF;

    slen = strlen(pMessage) ;
    pE = pMessage;
    do
    {
    pF = pE;
    leng = slen > MAXLEN ? MAXLEN : slen ;

    pE = pF + leng;

    while (*pE != ' ')
    {
    if (*pE == 0)
    break ;
    pE--;
    leng-- ;
    }

    leng==pE-pF ;

    printf("%*.*s\n", leng, leng, pF) ;
    getch();

    display(pF);
    slen -= leng;
    } while(slen>0);

    }
    this function is suppose to split my msg into 50 characters by 50 chars and display it. But i wish to store the segmented message into another pointer rather than directly displaying it. is there a way to do it?
    thanx for helping
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    You can create a pointer with new and for the char array size use the length of the char pointer you want to copy + 1. Then, you can use strcpy() to copy one char pointer to the other:

    pnew_string = new char[strlen(pstring) + 1];
    strcpy(pnew_string, pstring);

    pstring must contain a c-style string, i.e. one that's terminated with \0. (include <cstring>)

    If your pointer doesn't contain a c-style string, you can use a for-loop to copy one character at a time using pointer notation:

    pnew_string= new char[pstring_length];
    for(int i=0;i<pstring_length; i++)
    {
    *(pnew_string + i) = *(pstring + i);
    }
    Last edited by 7stud; March 20th, 2003 at 06:22 AM.

IMN logo majestic logo threadwatch logo seochat tools logo