0

I am having a difficulty trying to initialize an array of strings. I am working with one class that has different methods: one is intended to charge an array, other is intended to eliminate elements of an array and so on... I need to use the same array in every method inside the class.

The thing is that I need to make a simulation of the array(queue) and show its elements as they are added to or deleted from the queue and I am having problems trying to initialize the array to show nothing instead of random characters.

Here's the code of what I am trying to do...

class Queue
{
  public:
         char array[3][6]; //array of 3 positions holding up to 6 characters each
         int front, rear, max;

         Queue()
         {
             front=0;
             rear=0;
             max=3;
             array={"","",""}; //trying to initialize to blank spaces, here's the issue!
         }

         void add_element()
         { }

         void delete_element()
         { }

         void print_on_screen()
         { }
   };
   main()
   {  }

I have made it work by using a one dimension char array, entering a single number for each of the 3 positions but actually need it to work with more than a char.

PAT
  • 61
  • 1
  • 1
  • 9
  • Assuming C++03, http://stackoverflow.com/questions/2409819/c-constructor-initializer-for-arrays?rq=1 (and many more found in the related column on this page) – chris Apr 13 '13 at 00:04
  • not actually helpful :/ I read a couple suggestions before posting but they initialize a one dimension array... I am trying to initialize a two-dimension array – PAT Apr 13 '13 at 00:40
  • The fact still remains that you can't initialize array data members in C++03. Are you using C++11? – chris Apr 13 '13 at 00:42
  • nope... C++03. I read on the previous questions that you can initialize an array "the hard way" meaning to initialize position by position (array[0]="blah"; array[1]="blah"; array[2]="blah"), but that "hard way" doesn't work either, not for my 2D array at least :/ I guess I will give up on this method and try solving the issue in other way... – PAT Apr 13 '13 at 00:57
  • You should use either `strncpy` or `std::string` then. – chris Apr 13 '13 at 00:59

1 Answers1

0
char array[3][6]

is a two dimensional array of chars. This

array={"","",""};

is trying to initialize a size 3 one dimensional array of strings.

´a´ and "a" are different things. The first one is a char. The second is a string.

If you want to initialize the char array to something specific then you can do so like this

char array[3][3] = {{'a',' ','a'}, {'a','a','a'},{'a','a','a'}};
//                       ^^^
//                        blank space

However remember that initializing a char to blank space is not the same as initializing an int to 0. 0 is the default value for int. that is why usually ints are initialized to 0 when something is constructed. To signify that they still have the default value.

I think you should initialize it to zero. Like this.

     {
         front=0;
         rear=0;
         max=3;
         array[3][6] = {{0}};
     //                ^^^^  
     }

And finally "" is not blank space. This one is " "

  • Thanks for answering and for the advice... just tried initializing that way (with zero) but it doesn't work either :/ – PAT Apr 13 '13 at 00:26