Public Group

#### Archived

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

# an array of strings

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

## 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 on other sites
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

c++

##### Share on other sites
Then an "array" of strings should look like this:

  std::vector 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 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 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 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 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 on other sites
you can use dynamic allocation too

 ` char **szStrings;szStrings = new char*[Number_Of_Strings]; //alocate pointers to the stringsfor(int i = 0;i

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

##### 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.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632922
• Total Posts
3009216
• ### Who's Online (See full list)

There are no registered users currently online

×