basically a func named getString is supposed to ask user to enter a sentence, store sentence in the array, dynamically allocate a char array big enough to hold the sentence plus null terminator. then it should copy the sentence to the dynamically allocated array then return the pointer to the array.
i'm confused on the return statement and "ptr = getString(str, SIZE);"
Code:
#include<iostream>
#include<string>
using namespace std;
char getString(char *, int);
int main()
{
char *ptr;
const int SIZE = 81;
char str[SIZE];
ptr = getString(str, SIZE);
cout << ptr << endl << endl;
delete [] ptr;
ptr = NULL;
}
char getString(char *str, int size)
{
char *ptr;
int k=size;
cout << "enter a sentence.\n"; //ask user to enter a sentence
cin.get(str, size);
//dynamically allocate a char array just large enough to hold the sentence
//plus the null terminator.
ptr = new char[k+1];
//copy the sentence to the dynamically allocated array
//then return a pointer to the array
strcpy(ptr, str);
return char ptr;
}