-
October 31st, 2008, 04:49 AM
#1
question in strings
I want to write function that take array of strings and return to me one string that contain all of the words i passed to this function
i have problem with how to pass to function (prototype)
here is the main :
void main()
{
char *words[]={{"a"},{"picture"},{"is"},{"worth"},{"a"},{"thousand"},{"words"}};
char *word=sentence(words);
puts(word);
}
and the prototpye of sentence function is like this
char *sentence(char ** words);
is it ok? or wrong and how can i get the first word
is it *words[i] ?
best regards
-
October 31st, 2008, 05:49 AM
#2
Re: question in strings
1. In order to avoid confusion, let's talk of arrays of characters instead of strings of characters for "string" has a special meaning in C++.
2. Usually, fuctions in C return the EAX register, i.e. just a 32-bit value. This is quite small for a string a characters. It can return a pointer to an array, but that array needs to be reserved in memory and freed. It can not be placed on the (EDIT: stack), so it has to be a static variable or a dynamically allocated space with malloc() or new().
3. It is a good method to write functions returning only a status code (true or false, or an error number) and to pass arrays of characters as arguments that can be read or written (in and/or out).
So a solution could be
Code:
int make_sentence(char **words, int words_nb, char *sentence, int sentence_lg); // prototype
#define SENTENCE_MAX_LG 1000
char sentence[SENTENCE_MAX_LG];
int return_code = make_sentence(words, words_nb, sentence, SENTENCE_MAX_LG);
Last edited by olivthill; October 31st, 2008 at 09:52 AM.
-
October 31st, 2008, 08:02 AM
#3
Re: question in strings
ok tnx but i didnt understand why i can't aloocate char* pn the heap and as u write and make this way
char *sentence= new char[SENTENCE_MAX_LG];
int return_code = make_sentence(words, words_nb, sentence, SENTENCE_MAX_LG);
and as u said before in "make_sentence" function i fill sentence
why it must be static in main?
-
October 31st, 2008, 08:05 AM
#4
Re: question in strings
Originally Posted by olivthill
2. Usually, fuctions in C return the EAX register, i.e. just a 32-bit value. This is quite small for a string a characters. It can return a pointer to an array, but that array needs to be reserved in memory and freed. It can not be placed on the heap, so it has to be a static variable or a dynamically allocated space with malloc() or new().
Re-read that part of your post....
[hint: allocation on the heap in a program is done with malloc (for "C" programs) or new (for "C++" programs)]
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 31st, 2008, 08:12 AM
#5
Re: question in strings
Originally Posted by omryk
ok tnx but i didnt understand why i can't aloocate char* pn the heap and as u write and make this way
char *sentence= new char[SENTENCE_MAX_LG];
int return_code = make_sentence(words, words_nb, sentence, SENTENCE_MAX_LG);
and as u said before in "make_sentence" function i fill sentence
why it must be static in main?
You could do that, but unless SENTENCE_MAX_LG is absolutely enormous (to the point where the array would be too large for the stack), doing that gets you nothing, while increasing your complexity since you now are responsible for delete[]ing that array later.
If you wanted to use dynamic allocation for the output, it should be done *inside* the function, where you can decide how long of a string you need----the sum of all input words plus one, I think. In that case, of course, sentence would need to be passed as a char** so that you'd have a modifiable char* to work with.
Of course, this *entire* exercise would be about 500% easier if you used std::strings rather than C-style (char*) strings.
-
October 31st, 2008, 02:51 PM
#6
Re: question in strings
tnx but i started to lean c in the colledge and this is what i statrted to learn
-
October 31st, 2008, 03:04 PM
#7
Re: question in strings
Originally Posted by omryk
tnx but i started to lean c in the colledge and this is what i statrted to learn
Believe me, you could learn std::string in probably less time than it'd take to explain how to do what you're asking, and you'd save yourself huge amounts of time in the future.
-
October 31st, 2008, 03:11 PM
#8
Re: question in strings
Seriously, using std::string your entire sentence function would be:
Code:
std::string sentence(char ** words, int nwords)
{
int i;
std::string s;
for (i = 0; i < nwords; i++)
s += words[i] + " ";
return s;
}
-
October 31st, 2008, 04:33 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|