How to initialise char array?

This is a discussion on How to initialise char array? within the C++ Programming forums, part of the General Programming Boards category; How do I initialise a char array in a class definition? Code: class charArray { public: charArray() { charArray[30] = ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    How to initialise char array?

    How do I initialise a char array in a class definition?


    Code:
    class charArray
    {
    	public:
    		charArray()
    		{
                                           charArray[30] = {0};
    		}
    
    		void setSampleArray(char[] x){ sampleArray = x;} 
                                    
                                    char getSampleArray() {  return sampleArray; }
    
    
    	protected:
    		char sampleArray[30];
    
    };
    I know this code is horribly wrong but i dont know how to approach this.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This is completely wrong:
    Code:
    void setSampleArray(char[] x){ sampleArray = x;}
    You need to use strcpy() like this:
    Code:
    void setSampleArray(char[] x){ strcpy( sampleArray, x ); }
    Although you might want to use strncpy() instead to avoid writing past the end of your sampleArray.

    And this:
    Code:
    charArray[30] = {0};
    Won't initialize the whole array to NUL. If that's what you want, use memset().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    thanks for reply has helped but im getting a weird error with the line
    Code:
    void setSampleArray(char[] x){ strcpy( sampleArray, x ); }
    the compiler says missing ')' before identifier 'x' any ideas?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by te5la View Post
    thanks for reply has helped but im getting a weird error with the line
    Code:
    void setSampleArray(char[] x){ strcpy( sampleArray, x ); }
    the compiler says missing ')' before identifier 'x' any ideas?
    Oh of course, I should have seen that too.
    Code:
    char[] x
    should be:
    Code:
    char x[]
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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