Archived

This topic is now archived and is closed to further replies.

Few Simple Questions

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Few simple questions the answers of which I cannot do without (using vc++ 6.0) 1.) I have an array of strings...
char* strings[] = { string1, string2, string3};
... I know to access string1 you go strings[0]. But I need to access a particular character of that string through this array ( i need to access string1[4] for example but through the array because string1 will only exist in the array by that time). How would you go about doing that? strings[0][4]?? 2.) Also, is there a pause statement in DOS, which waits for the user to press enter before continuing execution? ------------------------------------------------------------ .:<<-v0d[KA]->>:.

Share this post


Link to post
Share on other sites
Well, your first question can be solved by doing this:

char message[]="Hello.";
cout << message[0]; //Prints ''H''

and system("pause") awaits a key stroke before continuing, or you could just use cin.getline().

Share this post


Link to post
Share on other sites
To access the fourth char in string number 2 for example would be

MyNewChar=*(String[2]+4);

Added: If you need the pointer instead of the value just drop the *

[edited by - Empirical on February 20, 2004 7:50:07 PM]

Share this post


Link to post
Share on other sites
Um, can''t you simply do this:
strings[0][3] 
in order to get the 4th character of the first string? I was really incredibly darned sure that you could. Of course, I''ve been using std::string for a while now, so I wouldn''t be surprised if I forgot tiny little details like that. But working with any array, not just character arrays, I figure you can always treat a double pointer as a two dimensional array. I''m like already 100% positive you can, and I should make a test program to test it out, but I''m too lazy.

Share this post


Link to post
Share on other sites
commander keen, actually, i used to say that, but ive been told its bad (yeah i know ) but yeah, go with system("PAUSE") (its not case sensitive if i remember right) according to the ppl on gamedev, its supperior, and if you dont know its header, its stdlib.h

Share this post


Link to post
Share on other sites
quote:
Original post by Agony
Um, can''t you simply do this:
strings[0][3]  
in order to get the 4th character of the first string?....


Well I never, so you can! Im still gonna use the methord im used to.

Anyone know if there is any diffrence between them?
(Other than Agony''s looks simpler)

Share this post


Link to post
Share on other sites
foo[bar] is just a pretty way of saying *(foo + bar), so in fact there is no difference between the two methods (yes, other than Agony''s being much easier to read).

In fact, based on that, and the basic properties of addition, this


int foo[10];

/* initialize foo with numbers here... */

std::cout << foo[5] << "\n";
std::cout << 5[foo] << std::endl;


will print the exact same value on both lines.

Not that I advocate that sort of behavior, but if you need job security....

Share this post


Link to post
Share on other sites