• Advertisement

Archived

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

an array of strings

This topic is 5814 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
fstream is the canonical example of why a) you shouldn''t use the old headers; and b) why you especially should never ever ever mix the old and new headers!

Want more details?
[C++ FAQ Lite] iostreams and cstdio
[vt.edu] New Style Issues
[Google] Old fstream, new fstream

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


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

or just use a char array...
quote:
- Similarly, do not use <tt>iostream.h</tt>, it is the ''old'' iostream library, available for compatibility only. Use <tt>iostream</tt> 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).

Have a look at the examples given for using a char array and then use the modern C++ approach. The latter is much more elegant, expandable, shorter and creates much less scope for errors. Why would you want to have to worry about buffer overruns, dynamic memory and array sizes when you don''t have to? There may be minor speed difference (which there won''t be if you put in all the proper error handling code), but unless your entire program consists of using a string vector I find it very hard to believe it will be a bottle neck.

The header issue is that, for this example, "#include <iostream>" puts all the identifiers in the file in the ::std namespace whereas "#include <iostream.h>" does not. In the same was you encapsulate related parts of code together in classes and files, namespaces do the same.

Share this post


Link to post
Share on other sites

  • Advertisement