Thread: [Dev-C++] Sentences in a array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Alexsandro M. <mei...@gm...> - 2005-04-26 05:00:29
|
Hi, dev-c++ users! Do you know if it is possible to put sentences in an array in the followin= g=20 way? array[0] =3D "sentence 1"; array[1] =3D "sentence 2" and so on... Any suggestion for this matter, i.e., to put sentences in a array? Tks in advance!!! --=20 Alexsandro Meireles |
|
From: Rafael C. <rc...@un...> - 2005-04-26 14:53:52
Attachments:
stringArray.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
|
|
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
|
|
From: David M. <dml...@co...> - 2005-04-30 17:32:36
|
Yes there are a couple ways to do this. string array[5]; char * array[5]; char array[5] [MAX_STRING_LEN]; in the first and last method you can just go ahead and use the space without a need to allocate any memory. The third way is actually quite wasteful unless you know exactly how long the strings are. For the second you will have to do something like: array[1] = new char[length of sentence + 1]; array[1] = "sentence"; //Once you are done with the sentence delete [] array[1]; That leaves the first method which requires that you be using C++ and not C, the latter 2 methods work in C as well. Hope this helps David McKen Alexsandro Meireles wrote: > 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 |
|
From: Per W. <pw...@ia...> - 2005-04-30 20:06:27
|
For the second alternative, it is possible to do (C code): array[1] = malloc(strlen(sentence)+1); if (array[1] != NULL) strcpy(array[1],sentence); or array[1] = "sentence"; note that no allocation is needed to just assign a pointer. However, the above two alternatives may not be combined into: array[1] = malloc(strlen(sentence)+1); array[1] = "sentence"; since the second assign will loose the reference to the memory previously allocated. /Per W On Sat, 30 Apr 2005, David McKen wrote: > Yes there are a couple ways to do this. > > string array[5]; > char * array[5]; > char array[5] [MAX_STRING_LEN]; > > in the first and last method you can just go ahead and use the space > without a need to allocate any memory. The third way is actually quite > wasteful unless you know exactly how long the strings are. For the > second you will have to do something like: > > array[1] = new char[length of sentence + 1]; > array[1] = "sentence"; > > //Once you are done with the sentence > delete [] array[1]; > > That leaves the first method which requires that you be using C++ and > not C, the latter 2 methods work in C as well. > > Hope this helps > David McKen > > Alexsandro Meireles wrote: > > > 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 |
|
From: Rafael C. <cha...@un...> - 2005-05-02 03:21:03
|
Per Westermark wrote:
>
> However, the above two alternatives may not be combined into:
> array[1] = malloc(strlen(sentence)+1);
> array[1] = "sentence";
> since the second assign will loose the reference to the memory previously
> allocated.
>
Per,
Thank you for remind us that!
Do you think it's safe using array[n]="string_literal" inside
functions?
<snip>
void foo (char *array[]){
array[0]="string_literal_1";
...
array[n]="string_literal_n";
}
</snip>
IMHO, for C code I'd use dynamic allocation with strings, since
it's not very common having only string literals. I mean, you don't
know exactly what you'll have, so you'll have to allocate memory.
Regards,
Rafael
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.11.0 - Release Date: 29/04/05
|
|
From: Per W. <pw...@ia...> - 2005-05-02 05:37:51
|
As you notice, it isn't so common to use only string literals, so most
often, dynamic memory is used.
It is a bad idea to create an array where some items are dynamically
allocated followed by a string-copy, and some entries are assignments of
pointers to string literals. The problem will be how to - in a portable
way - know which entries that requires release of dynamic memory.
However, the string literal method do have it's uses. Think for example
keywords for a computer language. All the keywords are know before, and to
speed up operation, the compiler may convert from a list of keywords to a
heavily optimized construct to maximize search time - for example a
perfect hash, where no keyword bin collides with any other keyword bin.
/Per W
On Mon, 2 May 2005, Rafael Charnovscki wrote:
> Per Westermark wrote:
> >
> > However, the above two alternatives may not be combined into:
> > array[1] = malloc(strlen(sentence)+1);
> > array[1] = "sentence";
> > since the second assign will loose the reference to the memory previously
> > allocated.
> >
> Per,
> Thank you for remind us that!
> Do you think it's safe using array[n]="string_literal" inside
> functions?
>
> <snip>
> void foo (char *array[]){
> array[0]="string_literal_1";
> ...
> array[n]="string_literal_n";
> }
> </snip>
>
> IMHO, for C code I'd use dynamic allocation with strings, since
> it's not very common having only string literals. I mean, you don't
> know exactly what you'll have, so you'll have to allocate memory.
>
> Regards,
> Rafael
>
>
> --
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.308 / Virus Database: 266.11.0 - Release Date: 29/04/05
>
|