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