Assignment of two char arrays

This is a discussion on Assignment of two char arrays within the C++ Programming forums, part of the General Programming Boards category; help please with this code, the compiler refuses the assignment, while i tried out to change it but hopless.....thanks in ...

  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
    359
    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,643
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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, 04:31 AM
  5. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 06:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21