Manipulating Character arrays in functions.

This is a discussion on Manipulating Character arrays in functions. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Got a quick one this time. Using a class Player (to store player name, etc) and having some ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Manipulating Character arrays in functions.

    Hi guys,

    Got a quick one this time.
    Using a class Player (to store player name, etc) and having some trouble passing arrays as parameters.
    In my definition of constructor, i have 2 arguments, first name and last name.
    Now i have to use char arrays to store names.
    In my Class Player, i have 2 private members, char arrays playerFirstName and playerLastName (both arrays hold 10 members).

    eg:

    private:

    char playerFirstName[10];
    char playerLastName[10];

    why doesn't the below constructor work?


    Code:
    Player::Player(char firstName[], char lastName[])
    {
    
    
       playerFirstName = firstName;
       playerLastName = lastName;
    
    
    
    }
    getting message: incompatible types in assignment of char* to char[10]...

    So does it want a pointer to an array or something along those lines?

    Regards,

    -Kirill

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You need strcpy to copy strings.

    Better, use std::string for all strings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Are you sure?

    1. I'm supposed to use char array (for assignment).
    2. I've done some quick testing, and the following works:

    Code:
    void displayArray(char toDisplay[])
    {
         
         cout<< "Your array is: " << toDisplay << endl;
         
         
         }
    
    
    
    
    
    int main()
    {
        
        
        char name[10];
        
        
        
        
        
        
        cin>> name;
        
       
        
        displayArray(name);
        system("PAUSE"); 
        
        
    }

    but when i use the same thing in a class with constructor (in my first post) it won't...
    confused..

    Regards,

    -Kirill

  4. #4
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Pointers and arrays are very closely related in C/C++. If you have
    Code:
    char a[128];
    char *b = a;
    the value of a is the address of the first element of the array. If you want the fifth element of the array, both a[5] and b[5] work, they are synonymous. If you wanted to pass a to some function, you would pass it as a pointer rather than an array. The 128 element array 'a' is 128 bytes wide and cannot be easily passed to a function. The 8 (or 4) byte pointer a can be passed just like any other parameter.

    Code:
    Player::Player(char *firstName, char *lastName)
    {
    
    
       strcpy(playerFirstName, firstName);
       strcpy(playerLastName, lastName);
    
    
    
    }
    That's what you want. Invoke the function in the same way that you were before (or use the string class as corned suggests)

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Ah yes, just tried something else in my testing code,

    so I can use the array (display it), but I can't copy from one array to another.

    eg:



    Code:
    char currentArray[10]; //create new array of 10 characters.
    
    
    void copyArray(char array[])
    {
    
         currentArray = array; //will not work
    
    }
    So how do i copy from one array to another?

    Regards,

    -Kirill

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kbro3 View Post
    So how do i copy from one array to another?
    See post #2 in this thread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by kbro3 View Post
    Ah yes, just tried something else in my testing code,

    so I can use the array (display it), but I can't copy from one array to another.

    eg:



    Code:
    char currentArray[10]; //create new array of 10 characters.
    
    
    void copyArray(char array[])
    {
    
         currentArray = array; //will not work
    
    }
    So how do i copy from one array to another?

    Regards,

    -Kirill
    strcpy for strings, memcpy for anything

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Or std::string since this is C++
    Then you can do an assignment, and be safe about it as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> strcpy for strings, memcpy for anything
    strcpy for C style strings, memcpy for any POD type. std::copy for anything copyable.

    >> Or std::string since this is C++
    I agree, but it sounds like the assignment requires character arrays.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    It's also better to use cin.getline if you can, since cin >> is unsafe for character arrays.
    The problem is that you can't copy an array by assigning it to somewhere else in C++.
    Another problem is that when you try to use an array in such way, you get a pointer to the first element, so you'd only be re-assigning the address in memory where the data is located, hence getting two variables "falsely" containing the same data.

    This might not make much sense to you (perhaps you don't understand what I just wrote), so remember to copy the string if this is your assignment. Pointers, if you haven't already learned them, comes later I'm guessing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    since cin >> is unsafe for character arrays.
    Use setw() to make it safe.
    Code:
    char buffer[SIZE + 1]; // Can hold SIZE characters plus the NUL.
    cin >> setw(SIZE) >> buffer;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    That's another way, I guess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 11:55 AM
  2. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  3. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  4. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 01:08 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 02:48 AM

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