Thread: Assignment of two char arrays

  1. #1
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Assignment of two char arrays

    help please with this code, the compiler refuses the assignment, while i tried out to change it but hopless.....thanks in advance


    Code:
      //............stuff
    
     vehicle::vehicle(char n[10]){ // defenition of constructor       
         this->name=n;                             
         cout<<"new vehicle was created its name is "<<endl;
         cout<<(this->name);
         return;
         };
    Programming is a high logical enjoyable art for both programer and user !!

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    as long as name is an array "this" should work

    Code:
     vehicle::vehicle(char n[10]){ // defenition of constructor       
         this->strcpy(name, n);                             
         cout<<"new vehicle was created its name is "<<endl;
         cout<<(this->name);
         return;
         };
    can't be sure though.
    Be a leader and not a follower.

  3. #3
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    in fact this ll not work as strcopy() is a memeber function of string.h , so it should be use with strings not with char array


    thats what i think, whatever it didnt work too

    thanks for trying help me
    Programming is a high logical enjoyable art for both programer and user !!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The *this* pointer is not used in this way. It's sole purpose is to return a pointer to an instance's "self". Look at it this way: In C, there were no undeclared instances, since you had to pass a structure as a parameter(otherwise make it global...). So it was easy as pie to assign a pointer to a named object. In C++, we use member functions, which of course, are "attached" to the instance calling them. Hence, how do you return a pointer to an unnamed object??! The *this* pointer was born. Now, if you find yourself trying to access specific members of the class with it, you are off-track. So in other words, you can completely omit it in your case.

    Here's an example of using it properly(albeit nonsensically...), then a simple rewrite of your code...



    Code:
    vehicle *ReturnIfInitialized() {
    
    if(name != NULL) return this;
    
    return NULL;
    }
    
    //............
    
    int main()
    {
    
    vehicle car(NULL);
    
    vehicle *ptr = car.ReturnIfInitialized() ;
    
    if( ptr == NULL)
    cout << "gimme a name!!" << endl;
    else
    cout << "Thanks for naming me" << ptr->name << "!!" << endl;
    
    getch();
    
    return 0;
    }
    
    //............



    Code:
    
    
    vehicle::vehicle(char n[10]){ // defenition of constructor       
         name=n;                             
         cout <<"new vehicle was created its name is "<<endl;
         cout << name;
         return;
         };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Smile

    thank you very much for ur detailed reply, which i found so friendly to my mind it was a real foolish from me to try use *This* in the constructor function........


    thanks again for ur help
    Programming is a high logical enjoyable art for both programer and user !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string tok to char arrays
    By simo_mon in forum C Programming
    Replies: 1
    Last Post: 03-16-2009, 02:07 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM