|
From: Verne H. B. <ve...@sy...> - 2005-04-26 15:42:09
|
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
|