Jump to content
  • Advertisement

Archived

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

graeme

an array of strings

This topic is 5990 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

is there a straightforward way to create an array of strings (which will all be the same length) i tried a few methods i saw searching on the net, but VC complains alot. the end result i want to achieve is an array of strings which specify file names (to be used in a loop to blit a background surface from tile map arrays) i guess the array ojects have to be cast as LPCSTR since i eventually want them to be processed by the ddutils.cpp function DDLoadBitmap() i''ts because strings are arrays of chars that i cannot simply declare: char bmps[8][8]; ??

Share this post


Link to post
Share on other sites
Advertisement
Which language are you using: C or C++?

--

The placement of a donkey's eyes in its head enables it to see all four feet at all times.


Edited by - SabreMan on February 22, 2002 5:37:49 PM

Share this post


Link to post
Share on other sites
Then an "array" of strings should look like this:

  
std::vector<std::string> v_s;


You add entries to it like this:

  
v_s.push_back( "stuff" );
v_s.push_back( "other stuff" );


And access the entries like this:

  
cout << v_s[0] << "\n";
cout << v_s[1] << "\n";


You will need the headers <vector> and <string> for vector and string. In other words, if you''re going to use C++, then write C++, not C.

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
this is sounding good, but i have no vector.h on my machine;
searching on google i found a couple, and including them in the project gave me the impression that they weren''t the vector.h you had in mind, what with the errors that came about when i compiled...

where would i find vector.h that will make the code work?

and why can''t an array of string literals be declared as char blah [n][n]? you used inverted commas around "array" as if it perhaps what i am trying to describe isn''t really an array, am i missing some fundamental point?

thanks for trying to help, mr S

Share this post


Link to post
Share on other sites
you can use a 2D char array... you must remember to both leave space for a NULL at the end of each, and also put that NULL in there:
#include < iostream.h>
int main(int argc, char* argv[])
{
char str[8][8];
char da[15] = "abcdefghijklmn";
for (int x = 0; x < 7; ++x)
for (int y = 0; y < 7; ++y)
str[x][y] = da[x + y];
for (int t = 0; t < 7; ++t)
{
str[t][7] = ''\0'';
cout << t << " " << str[t] << "\n";
};
return 1;
};

LPSTR just means char* more or less...

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
- If you have a C++ compiler, you ought to have the vector header file. Note that it is in vector and not in vector.h

- If a function requires a const C string (const char*, or a litteral "string"), pass use string::c_str(). If the C function needs to modify the string (i.e. expects a char[] buffer), use std::vector buf(size) and pass the address of the first element &buf[0] for the char*

- Similarly, do not use iostream.h, it is the ''old'' iostream library, available for compatibility only. Use iostream instead.

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
- If a function requires a const C string (const char*, or a litteral "string"), pass use string::c_str(). If the C function needs to modify the string (i.e. expects a char[] buffer), use std::vector buf(size) and pass the address of the first element &buf[0] for the char*

or just use a char array...
quote:
- Similarly, do not use iostream.h, it is the ''old'' iostream library, available for compatibility only. Use iostream instead.

out of curiosity, what is the difference (besides that namespace crap)? i hear this all the time but nobody ever says why (except for "it''s newer", which is hardly a valid reason).

Share this post


Link to post
Share on other sites
you can use dynamic allocation too

    
char **szStrings;
szStrings = new char*[Number_Of_Strings]; //alocate pointers to the strings

for(int i = 0;i<Number_Of_Strings;++i)
{
szStrings[i] = new char[Max_String_Length];// alocate each string

}

// when you are done, delete:


for(int i = 0;i<Number_Of_Strings;++i)
{
delete [] szStrings[i];// delete each string first

}
delete [] szStrings; //Finally delete array of pointers.





Edited by - kwizatz on February 22, 2002 9:10:07 PM

Share this post


Link to post
Share on other sites
quote:
Original post by krez
out of curiosity, what is the difference (besides that namespace crap)? i hear this all the time but nobody ever says why (except for "it''s newer", which is hardly a valid reason).



Well, how can I explain... it is not the same library...
The iostream.h heads dates from the time when C++ was "C with classes" (i.e. pre-standardisation). Their implemetation is not the same... and may break your code if you rely on the documentation for the new library.

Here''s a link.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!