Thread: RE: [Dev-C++] Sentences in a array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Mat...@Ve...> - 2005-04-26 15:31:24
|
Yes, using the <string> header, you get a string type. std::string array[50]; array[0]="foo bar baz boo"; array[1]="qux quux quuux quuuux"; ...and so on! The cool thing with string is that they're much easier to deal with than char* . They resize themselves automatically so you don't have to worry about the pointers yourself. HTH! Matt -----Original Message----- From: dev...@li... [mailto:dev...@li...]On Behalf Of Alexsandro Meireles Sent: Monday, April 25, 2005 10:00 PM To: Dev C++ Subject: [Dev-C++] Sentences in a array Hi, dev-c++ users! Do you know if it is possible to put sentences in an array in the following way? array[0] = "sentence 1"; array[1] = "sentence 2" and so on... Any suggestion for this matter, i.e., to put sentences in a array? Tks in advance!!! -- Alexsandro Meireles ___________________________________________________________________ The information contained in this message and any attachment may be proprietary, confidential, and privileged or subject to the work product doctrine and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to this message and deleting it and all copies and backups thereof. Thank you. |
|
From: <Mat...@Ve...> - 2005-04-26 16:02:27
|
Yes, it works, but char* is not a very good thing to use any more. It
creates arbitrary limits on lengths that lead to bugs and security problems.
On top of this, you get a minimal performance increase because C++ compilers
and compiler optimization are a great deal better than they used to be not
long ago.
The whole point of C++ is avoiding these issues with generic programming and
classes. Best of all the performance hit is minimal. If you look at
basically every other language in use today besides C, strings are handled
automatically.
GAWK, perl, C++, etc. all do it using string types.
Sometimes it's not good enough for it just to work. There are other factors
to consider.
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Verne H.
Bohlender
Sent: Saturday, February 26, 2005 8:42 AM
To: Dev C++; Rafael Charnovscki
Subject: Re: [Dev-C++] Sentences in a array
The program works fine once you use what comes with Dev C++ and not
substitued the headings. I just clipped the code and used the console
settings and it worked perfectly. Now I'm not sure what he wants it to do
but it gave me no problems at all.
Verne the real amateur at C++
> > Alexsandro Meireles wrote:
>> array[0] = "sentence 1";
>> array[1] = "sentence 2"
>> and so on...
>
> There are some ways I can remember.
> The point is defining your array as
> an array to char pointers.
>
> //initializing an array of strings
> char* sentences[2]={"sentence 1", "sentence 2"};
>
> //assigning strings to the array
> char* sentences[2];
> sentences[0]="another sentence 1";
> sentences[1]="another sentence 2";
>
> /* allocating memory dinamically and copying
> the string */
> strings[0]=(char *)malloc(sentenceLength + 1);
> strcpy(strings[0],"sentence 1 again");
>
> It could be also an bidimensional array of char, but
> I'd rather use pointers:
> char sentences[20][101]; //20 sentences with a limit of 100 characters
>
> Maybe other developers have more suggestions.
> I sent a program (attached) just as an example.
>
> Hope it helps,
> Rafael
>
----------------------------------------------------------------------------
----
> #include <stdlib.h>
> #include <string.h>
>
> int main (){
> //initializing an array of strings
> char* sentences[2]={"sentence 1", "sentence 2"};
> char* strings[2];
> int sentenceLength; //(number of characters of your sentence)
> int i;
> for (i=0;i<2;i++)
> printf("%s\n", sentences[i]);
> system("PAUSE");
> sentences[0]="another sentence 1";
> sentences[1]="another sentence 2";
> for (i=0;i<2;i++)
> printf("%s\n", sentences[i]);
> system("PAUSE");
> sentenceLength = 30; //just an example
> strings[0]=(char *)malloc(sentenceLength + 1);
> strcpy(strings[0],"sentence 1 again");
> sentenceLength = 60; //just an example
> strings[1]=(char *)malloc(sentenceLength + 1);
> strcpy(strings[1],"sentence 2 again");
> for (i=0;i<2;i++)
> printf("%s\n", strings[i]);
> system("PAUSE");
> return 0;
> }
>
>
>
----------------------------------------------------------------------------
----
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.10.3 - Release Date: 25/4/2005
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
___________________________________________________________________
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
|
|
From: Per W. <pw...@ia...> - 2005-04-26 16:35:03
|
When talking about performance, it isn't the quality of the C++ compiler
that is affecting the runtime. It's the algorithmic differences that
matter - number of memory allocations, or the number of string copies.
A number of memory allocations needed when using std::string can be
avoided when using char[] constructs - a lot of strings do have a known
maximum length.
On the other hand, std::string can perform assigns from other std::string
objects without allocating/copying any data, since multiple std::string
objects can share a common string buffer.
/Per W
On Tue, 26 Apr 2005 Mat...@Ve... wrote:
> Yes, it works, but char* is not a very good thing to use any more. It
> creates arbitrary limits on lengths that lead to bugs and security problems.
> On top of this, you get a minimal performance increase because C++ compilers
> and compiler optimization are a great deal better than they used to be not
> long ago.
>
> The whole point of C++ is avoiding these issues with generic programming and
> classes. Best of all the performance hit is minimal. If you look at
> basically every other language in use today besides C, strings are handled
> automatically.
>
> GAWK, perl, C++, etc. all do it using string types.
>
> Sometimes it's not good enough for it just to work. There are other factors
> to consider.
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Verne H.
> Bohlender
> Sent: Saturday, February 26, 2005 8:42 AM
> To: Dev C++; Rafael Charnovscki
> Subject: Re: [Dev-C++] Sentences in a array
>
>
> The program works fine once you use what comes with Dev C++ and not
> substitued the headings. I just clipped the code and used the console
> settings and it worked perfectly. Now I'm not sure what he wants it to do
> but it gave me no problems at all.
> Verne the real amateur at C++
>
> > > Alexsandro Meireles wrote:
> >> array[0] = "sentence 1";
> >> array[1] = "sentence 2"
> >> and so on...
> >
> > There are some ways I can remember.
> > The point is defining your array as
> > an array to char pointers.
> >
> > //initializing an array of strings
> > char* sentences[2]={"sentence 1", "sentence 2"};
> >
> > //assigning strings to the array
> > char* sentences[2];
> > sentences[0]="another sentence 1";
> > sentences[1]="another sentence 2";
> >
> > /* allocating memory dinamically and copying
> > the string */
> > strings[0]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[0],"sentence 1 again");
> >
> > It could be also an bidimensional array of char, but
> > I'd rather use pointers:
> > char sentences[20][101]; //20 sentences with a limit of 100 characters
> >
> > Maybe other developers have more suggestions.
> > I sent a program (attached) just as an example.
> >
> > Hope it helps,
> > Rafael
> >
>
>
> ----------------------------------------------------------------------------
> ----
>
>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > int main (){
> > //initializing an array of strings
> > char* sentences[2]={"sentence 1", "sentence 2"};
> > char* strings[2];
> > int sentenceLength; //(number of characters of your sentence)
> > int i;
> > for (i=0;i<2;i++)
> > printf("%s\n", sentences[i]);
> > system("PAUSE");
> > sentences[0]="another sentence 1";
> > sentences[1]="another sentence 2";
> > for (i=0;i<2;i++)
> > printf("%s\n", sentences[i]);
> > system("PAUSE");
> > sentenceLength = 30; //just an example
> > strings[0]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[0],"sentence 1 again");
> > sentenceLength = 60; //just an example
> > strings[1]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[1],"sentence 2 again");
> > for (i=0;i<2;i++)
> > printf("%s\n", strings[i]);
> > system("PAUSE");
> > return 0;
> > }
> >
> >
> >
>
>
> ----------------------------------------------------------------------------
> ----
>
>
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.308 / Virus Database: 266.10.3 - Release Date: 25/4/2005
|