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