char array with pointer multiple lines of text

closed account (1CfG1hU5)
how to store multiple lines of text in a char array and display to the screen?

no help needed. this is the question and the answer. putting this here
for beginners as an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
char *text[] = {
  "message0",
  "message1",
  "message2"
};

can also use char near *text[], char far *text[], char huge *text[].
has to have an asterisk pointer to display each element of the char array.

for loop example to display text:

char i; // int type can also be used
for (i = 0, i < 3; i++)
  printf("%s\n", text[i]);


while loop example:

char i = 0;  // may not be zero when declared as "char i;" only
while (i < 3) {
  printf("%s\n", text[i]);
  i++;
 }


do while loop example:

char i = 0;  // may not be zero when declared as "char i;" only
do {
 printf("%s\n", text[i]);
 i++;
} while (i < 3);


Last edited on
1) This will soon get washed off from 1st page and nobody will see it
2) can also use char near *text[], char far *text[], char huge *text[]. you can't. At least in standard C++.
3) It is const char* []. Get const-correctness right!
closed account (1CfG1hU5)
MiiNiPaa,

about 1)
will be findable by search.

about 2)
maybe newer programming languages set the memory size with "char *name[]" automatic.

in the dos language near, far, and huge are specifiable. near is 16-bit, far and huge are 32-bit.

about 3)
maybe "const char*" is necessary in your programming language.

for the dos language, const means the text won't change, but isn't really necessary
since the text isn't modified by a loop. so the format i list is correct.

you didn't list a name for the char array as "const char* []"
char is a keyword and isn't enough.
Last edited on
for the dos language
There is no dos language.
different kids of pointers are unstandard extension for segmented memory model in C. There is noo such keywords in C++

maybe "const char*" is necessary in your programming language.
Note that this is a C++ forum. It is language of everyone here. And it does not allows char* pointers to string literals. C does allows it and it was a C design error because for language to allow modification of the read only memory is type safety violation.

And most importantly: On desktops segmented model is dead. Dos is dead. Pre-standard C++ is dead. Stop living in previous century.
closed account (1CfG1hU5)
there is a dos language:

the language is called "turbo c++ 1.01". there is also a 3.0 version. probably was a 2.0 also. because the language is old does not mean it does not exist.

the type safe violation is in the newer language(s), not this older dos one.

in turbo c++ 1.01 I can do the char array listed in 1st message here and works with for, while, and do while loops. the bgidemo.c that came with the language also has this format and does work.

here is an example from the demo of this most likely abandonware programming language:

char *Fonts[] = {
"DefaultFont", "TriplexFont", "SmallFont",
"SansSerifFont", "GothicFont"
};

each text in quotes is an array element.

dos isn't completely dead. turbo c++ 3.0 dos is used to compile freedos objects.

related to centuries, make your own decisions. not mine.

if dos were dead, there wouldn't be dosbox on sourceforge.net
Last edited on
closed account (1CfG1hU5)
windows 7 has xpmode which has a dos shell that can run dos apps :)

windows 8 can probably do same.

when did dos die?
Last edited on
Topic archived. No new replies allowed.