returning an array using pointer

#include <iostream>

using namespace std;

char * userInput()

int main()
{
int i;
char filterInput[80];
char *(getInput)[80] = userInput();
cout<<"0: "<<getInput[4]<<endl;

for(i=0;i<80;i++){
if(getInput[i] == '\0'){
break;
}//end if
else{
filterInput[i] = getInput[i];
cout<<"filterInput"<<i<<" "<<filterInput[i]<<endl;
}
}//end for
cout<<"sd: "<<filterInput<<endl;
// braceCheck(filterInput[]);
cin.get();
cin.get();
cin.get();
cin.get();

}//end main

char * userInput()
{

char checkInput[80];
cout<<"Input Anything: ";
cin>>checkInput;
if((checkInput[0]=='e')&&(checkInput[1]=='x')&&(checkInput[2]=='i')&&(checkInput[3]=='t')){
exit (0); //terminates the program
}//end if
else{
char * character = checkInput;
return (character);
}//end else
}//end userInput function


-----------------------------------------------------------------------
what is the proper way of returning an array using a pointer?
I need to pass the array in userInput function to main.
How? I'm still confused with pointers to arrays thx.
Can someone edit my code to make it run properly? thx.
what is the proper way of returning an array using a pointer?


Generally, there isn't one. You don't return arrays.

You're better off passing a pointer to the buffer to the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void userInput(char* checkInput)
{
  // do stuff here
  // write to checkInput as if it were a normal array
}

int main()
{
  char input[80];

  // get user input
  userInput( input );

  // stuff here
}


Although -- screw char arrays. Save yourself the headaches and buffer overflows and use std::string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string userInput()
{
  string checkInput;

  // do stuff with checkInput here

  return checkInput;
}

int main()
{
  string input = userInput;

  // etc
}
whoa! thank you very much Disch.
I never thought about that!
Topic archived. No new replies allowed.