0

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 really confused on the return statement and "ptr = getString(str, SIZE);" i keep getting this error: "cannot convert from 'char' to 'char*' ".....which is related to "ptr = getString(str, SIZE);"

#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 *ptr; 
        //return char *ptr;
       // return ptr;

}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Salem
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.