![]() |
Returning An Array Of Characters
How can I return an array of characters from a function? I know there's always the option of returning a pointer, but when I try that, I'm probably doing it wrong because I have to use a for loop to reference the address of each individual pointer.
It'd be easiest to do it this way as well, so that I can just cout the function call, without needing to do anything to it. :
#include <iostream> |
>How can I return an array of characters from a function?
You can't. Either return a pointer, or wrap the array in an object so that it can be copied as a single entity. >I'm probably doing it wrong Yes. Yes you are. :
int get_size(char text[MAX_SIZE]):
int get_size ( char text[] )Tell me, what's wrong with the following code: :
char* replace(...)>cin.sync(); That's not guaranteed to do what you think. By the way, you're commenting the wrong parts of your code. Anything that isn't immediately obvious should have a comment, or be changed so that it's immediately obvious. The one thing I wondered while reading your code is precisely the comments you should have added: why are you reinventing the wheel when the standard library supports a huge portion of what you're trying to do? |
Quote:
http://www.programmingforums.org/for...36&postcount=5 |
Narue and Cache have answered your question quite well. I'll just add a few little specific notes;
1) Your functions are essentially recreating the wheel of the C standard library. For example, your get_size() function achieves more or less the same thing as the strlen() function that is specified in <string.h>. Except that you are adding a few other quirks that aren't particularly useful. 2) Since this is a C++ group you might want to use the standard string class (std::string which is specified in the header <string>). This is (rather loosely) an object wrapper for an array of char. And can be passed to an output stream directly. |
Quote:
Thank you for the help. By the way, Narue, I'm not sure if you were calling my approach to it incorrect. Because all I had to do was add the word static before the declaration of "new_text". Which is still returning a pointer, but it's not as if I had to do much. |
Quote:
|
>By the way, Narue, I'm not sure if you were calling my approach to it incorrect.
Your approach was broken, and therefore incorrect. I thought I made that abundantly clear. >Because all I had to do was add the word static before the declaration of "new_text". You'll notice that I went out of my way to mention that a static local variable has pitfalls. Unsurprisingly, you've chosen to pick the worst of your options (next to the undefined behavior you already had) and learn for yourself why it's a bad idea. That's really the best way to learn, in my opinion. :) >Which is still returning a pointer, but it's not as if I had to do much. In all honesty, I feel that your design sucks and needs to be scuttled. But rather than focus on subjective topics, I gave you concrete advice on the assumption that you would find my questioning of your design as an insult. People tend not to take "delete it all and start over" very well, so I reserve that for particularly awful code and real life subordinates. ;) |
Quote:
|
Quote:
Besides the lack of comments, would you please tell me what needs to be done to derail it of its "awfully designed" state? ... Without using the C standard functions that this is intended to replicate? :
#include <iostream> |
OK; just remember you asked.
I am ignoring the fact that we could probably do all this with a few lines using either C or C++ standard library features, and that your comments don't help much for explaining what your code is doing. I also ignore the fact that you are using the C++ standard library by using <iostream>. 1) Narue's already mentioned the issue of hard-coding array sizes in function declarations. 2) It's not really all that obvious, without a close look, what i1, i2, and size_f are being used for in your locate() function. That gives you a maintenance problem if you pick this code up in a few months and need to improve it. 3) You have multiple return points from your locate() function. Again, that gives you a maintenance problem if you pick this code up in a few months and need to improve it. 4) Minor nit (as compilers will cope with this): you're using postincrement when preincrement will do the job. 5) It is not obvious what the variables offs1, offs2, i1, i2, i3 are being used for in replace(). Yes, I know offs1 and offs2 are offsets, but that only is obvious after looking at the rest of the code. 6) The size of your new_text array (or memory allocated) needs to be larger than MAX_SIZE 7) size_t is a type that is used frequently in both the C and C++ standard libraries. You are using it as a variable name, which means obscure incompatibilities with either the C or C++ standard libraries .... and you are using <iostream>. 8) Your replace() function dynamically allocates memory, and requires the caller to release it. This is an opportunity for the caller to experience a memory leak. Your main function fully exploits that opportunity by calling replace() repeatedly in a loop, and never releasing the dynamically allocated memory. 9) Minor: "if (text[i1] == '\0')" can be simplified to "if (text[i])" 10) Why, in main(), declare from and to as being of size MAX_SIZE when you are assigning their contents using string literals? 11) You main() function declares text as a pointer, and assigns it to new char [MAX_SIZE]. Then you reassign text to a string literal, and therefore another memory leak. 12) "if (!(where == -1))" is more readably expressed as "if (where != -1)" 13) As Narue mentioned, cin.sync() and cin.get() probably aren't doing what you intend, even if it appears they are. I haven't looked all that closely, so there are probably some other issues I've missed. |
| All times are GMT -5. The time now is 1:56 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC