an array of strings
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]; ??
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
--
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
Then an "array" of strings should look like this:
You add entries to it like this:
And access the entries like this:
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.
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.
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
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
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:
LPSTR just means char* more or less...
--- krez (krezisback@aol.com)
#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)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
- 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.
- 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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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).
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
you can use dynamic allocation too
Edited by - kwizatz on February 22, 2002 9:10:07 PM
char **szStrings;szStrings = new char*[Number_Of_Strings]; //alocate pointers to the stringsfor(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
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement