Re: [Dev-C++] How do I pass arrays of data from a function?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-10-21 22:32:15
|
A number of problems here, but that is what learning is about :-)
Number 1.
You can never return the address of a variable declared inside a function
UNLESS that variable is declared static, i.e.
char* my_function() {
static char buf[100];
strcpy(buf,"Hello world");
return buf;
}
Number 2.
Your keybd_input is returning a character (the first character in your
array, since return *getKeyString is the same as return getKeyString[0].
The function should be declared:
const char* keybd_input() {
static char getKeyString[100];
....
return getKeyString;
// which is the same as
// return &getKeyString[0]
// i.e. return a pointer to the first character in your array.
}
Number 3.
Whenever you fill a character array with characters, you should either
ALWAYS use your length variable (x) to keep track of number of valid
characters in the buffer, or you should end your input by inserting a
"termination" character at the end, i.e. directly before the return, you
should write:
getKeyString[x] = 0;
// Or possibly: getKeyString[x] = '\0';
Number 4.
Whenever you are filling a finite size (aren't they always) buffer you
should keep track of the available size of your buffer, i.e. a buffer of
size 100 may only store 99 characters plus the terminating zero.
while ((getKeyString[x] = getchar()) != '\n' && x < 99) {
x++;
}
Number 5.
Since you can't have a function return an array of characters, only a
single character, or a pointer to a character, the caller of the function
must perform the copy of the characters to the destination array:
ShellCount = 0;
....
while (some_break_criteria) {
strcpy(ShellName[ShellCount++],keybd_input());
if (ShellCount >= (sizeof(ShellName)/sizeof(ShellName[0])))
// Have consumed all my 4096 shell strings now. Have to end
// receiving more strings.
break;
}
Number 6.
There isn't any need to reopen that poor a_file for each line you are
about to write out. Open it before the for loop, and close it after the
for loop.
{
ofstream a_file(...);
for (;;) {
}
a_file.close();
}
And the close shouldn't be needed if a_file is defined within a local
scoope, since it will be automatically closed when a_file leaves the
scope, and the destructor is run.
Number 7.
You have to give the program a chance to terminate the input loop somehow.
Maybe you should add a check if the user has pressed enter without
specifying any characters. It might be a bit bothersome if the user has to
enter 4096 separate commands before the program ends.
I home these comments helps you somewhat on the way. Pointers and arrays
does have a way of making the life miserable for C or C++ programmers :-)
/Per W
On Mon, 21 Oct 2002, Rob Peacock wrote:
> Hi all,
>
> I'm in the process of trying to teach myself C and C++ but don't have
> anyone to ask questions. I hope you all can answer them.
>
> I am using Dev-C++ (4.9.6.0) on NT4 SP6a.
>
> I am collecting a character stream from the keyboard and storing it in a
> character array from within a function. When I try to pass this back up to
> the calling program, I want to put this data also into an array.
>
> Here is the code I have :
>
> parts of the program that call the function:
>
> /* fn() prototypes */
> char keybd_input();
>
> int main()
> {
> char ShellName[4096][100];
> char getShellName[100];
>
> cout<<"Please enter the shell name: ";
> *getShellName = keybd_input(); // get the keyboard characters
> ShellName[x] = getShellName; // is this the where I'm doing it wrong?
> cout<<"ShellName["<<x<<"] = "<<ShellName[x]<<endl;
>
> for (x=0; x<ShellCount; x++)
> {
> ofstream a_file(getFileName, ios::app); /* opens a text file to append
> to */
> a_file<<ShellIndex[x]<<"\t";
> a_file<<"\n";
> a_file.close();
> }
> }
>
> the function:
>
> char keybd_input()
> {
> int x ; // counter var
> char getKeyString[100]; // character array from keyboard
>
> cout<<"Please enter a long string: ";
>
> x=0;
> while(( getKeyString[x] = getchar() ) != '\n')
> {
> getKeyString[x];
> x++;
> }
> return *getKeyString;
> }
>
>
> My problem is that I am only getting the first character of the data passed
> back from the function.
>
> Any ideas on how to fix this?
> Your help would be appreciated.
>
> Thanks,
>
> --->Rob
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ad.doubleclick.net/clk;4699841;7576298;k?http://www.sun.com/javavote
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|