Thread: pointers(please help)

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    pointers(please help)

    I have written a program (thanks ammar) but it is hard to understand this pointers. Can some one explain me how the pointers in this program works. Pointers make me sick it is hard to understand it and I want to understand it deeply. Thanks a lot.

    ----------------------------------------------------------------------------------

    #include <iostream>
    using namespace std;
    class str
    {
    private:
    char * st;
    int len;
    public:
    str(char *ch);
    void display(void);
    int length(void);
    str reverse(void);
    str(const char * &);
    str concat(str);
    ~str();
    };

    //-------------------------------------------------------
    str::str(char *ch)
    {
    for (int i=0;ch[i]!='\0';i++);
    st=new char (i);

    int k=0;
    while(ch[k]!='\0'){
    k++;
    }
    len=k;

    for (int j=0;j<len;j++)
    {
    st[j]=ch[j];
    }
    st[j]='\0';
    }

    str str::concat(str a){

    char temp[1000];
    strcpy(temp,st);
    free(st);
    st = strlen(a.st)+strlen(temp)+1;
    for (int j=0;j=strlen(temp);j++)
    {
    st[j]=temp[j];
    }

    for (int i=0;i<strlen(a.st);i++)
    {
    st[i]=a.st[i];
    }

    // or we can use strcat(st,a);
    return *a,

    }
    //--------------------------------------
    str::~str(){
    delete[] st;
    }
    // --------------------------------------

    str::str(const char * & inobj){

    for (int i=0;inobj[i]!='\0';i++);
    st=new char (i);

    int k=0;
    while(inobj[k]!='\0'){
    k++;
    }
    len=k;

    for (int j=0;j<len;j++)
    {
    st[j]=inobj[j];
    }
    st[j]='\0';
    }


    //--------------------------------------------------------
    void str::display(void)
    {
    int i=0;
    while(st[i]!='\0')
    {
    cout<<st[i];
    i++;
    }
    cout<<endl;
    }
    //---------------------------------------------------------
    int str::length(void)
    {

    return len;
    }
    //---------------------------------------------------------

    str str::reverse(void)
    {
    str rst;
    for(int i=0;i<len;i++);
    rst.st =new char (i);

    for (int j=(len-1);j>=0;j--)
    {
    rst.st[len-1-j]=st[j];
    }

    rst.st[len]='\0';

    return rst;
    }

    //---------------------------------------------------------
    int main()
    {
    str string1("cse");
    string1.display();
    str string2=string1.reverse();
    string2.display();
    str string3=string1.concat(string2);
    return 0;
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Iīm not going to go through every line what happening but rather give a brief description. In order to understand this code "deeply" you must have some knowledge of the relationship between pointers and arrays and pointer arithmetric (I suggest make a search on this board or on google)

    The provider of this code is trying to create(emulate) a string-class.

    The constructor allocates dynamic memory (deep copy, itīs called I think) so it can store the string in the char st pointer. The length is determined here. Ps you have 2 version of constructors and therefore it is overloaded.

    The member function concat actually concrates(or add) another string. This is accomplished by freeing memory that was previsosly allocated by st and create a new one based on argument passed + the old string. A new length is calculated here.

    The destructor free the dynamic memory allocated by st.

    The member function reverse just flip the string. It creates a tempstr(ing) that is a copy of the actual string. Then it assigns st backwards from tempstr(ing).

    I hope I didnīt confuse you!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    How did you develop an application and not understand its design and how it was implemented?

    Look over the design.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pointers are one of the hardest concepts to understand in C/C++. I would encourage you to use a textbook or a good tutorial to help you get started. Online I think Liberty's Teach Yourself C++ in 21 days is best one available if you don't already know C. If you know C, then Bruce Eckels Thinking in C++ has a lot going for it as well. Learning how to use pointers is hard enough when using a reputable textbook, let alone trying to do it piecemeal.

    Here's a few tips. Pointers are like other variables in that they have to be created using a given type, they have a name, they have an address, and they contain information. The information they contain is the address of another variable/function. Therefore, trying to assign an int to a pointer as in the following line from your code won't accomplish the purpose you are trying:

    st = strlen(a.st)+strlen(temp)+1;


    Pointers can point to memory on the stack or on the heap (aka free store, dynamic memory, whatever you call it). However, if you assign dynamic memory to the pointer then you need to follow the appropriate protocol to release it. If you allocate the memory with the new operator then you need to release it with the delete operator and if you allocate it with malloc then you release it with free. Don't mix and match as you do in the program, however.

    Because using pointers is difficult I would encourage you to learn how to comment your code. If you do so then you can pick up other booboos like below:

    for (int i=0;ch[i]!='\0';i++);
    st=new char (i);

    I think you are trying to determine how much memory to allocate to str so you can assign the value of ch to str later in the function, but withought comments I can't be sure. Assuming that is the intent, then you don't want the semi-colon after the closing brace of the for loop as it then makes the for loop irrelevant. Likewise, the second line is a little unclear. Generally the syntax is:

    st = new char;

    for a pointer to a single char and

    st = new char[i];

    for a pointer to a char array of size i. The new operator will create one or more objects of the type indicated using a constructor of the type indicated. The default constructor will be used for arrays and the default constructor will be used by default for individual objects. You can indicate a different constructor by passing arguments to a non-default constructor of a user defined class as desired:

    struct car
    {
    car(int);
    //etc.
    };

    car *pCar = new car(4);

    calls the non-default constructor for class car passing it the value of 4. However, primitive types like char don't have non-default contstructors. Therefore the syntax of the second line of the last code snippet is off somehow, depending on what you are trying to do. The simplest sequence I can think of would be this:

    Code:
    //constructor using an input string represented by a char pointer
    str::str(char *ch)
    {
      //determine length of input string
       length = strlen(ch);
       
      //assign adequate memory to st to hold input string
       st = new char[length + 1];
    
      //copy input string to st
       strcpy(st, ch);
    }

Popular pages Recent additions subscribe to a feed