Array C++

I am trying to "display the contetns of the array one character at a time using a simple loop" Now to display an array, you have to have a capacity that the number given to it is the bytes in total. "This is the test message. It has a couple sentences in it." In the message above their are 48 letter characters for each. Do i need a seprate byte for each of them?
Last edited on
have a capacity that the number given to it is the bytes in total.
That is only true for characters. The correct concept is the array has the total number of elements.

eg int x [1000]; //not 1000 bytes. for 64 bit integers, that is 64000 bytes!


Yes, you need a byte for each letter. For C-style strings (which you do not appear to be doing at this time) you need 1 more for a hidden 'end of string' byte (the 0 value byte). It would be a lot easier for you if you DO add the extra byte.


so you can do this
char test[49] = "This is the test message. It has a couple sentences in it.";
// the above array HAS one slot for each letter, that is what the 49 does. the 49 allocates 49 characters (yes, here, they are bytes) for you. It knows to put the hidden 0 on. If you allocated it too short, this would be a problem. and if you did not want the extra zero, you have to do it manually (or put in part of the string and correct it manually)
char test2[48] = {'T','h','i','s',' ', …. ;

for(dx = 0; dx < 49; dx++) cout << test[dx];

you can also do this, because of the extra zero:
cout << test;

Last edited on
Topic archived. No new replies allowed.